You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
222 lines
6.6 KiB
222 lines
6.6 KiB
|
2 years ago
|
// ScreenshotHelper.ts
|
||
|
|
|
||
|
|
import path from "node:path"
|
||
|
|
import fs from "node:fs"
|
||
|
|
import { app } from "electron"
|
||
|
|
import { v4 as uuidv4 } from "uuid"
|
||
|
|
import { execFile } from "child_process"
|
||
|
|
import { promisify } from "util"
|
||
|
|
import screenshot from "screenshot-desktop"
|
||
|
|
|
||
|
|
const execFileAsync = promisify(execFile)
|
||
|
|
|
||
|
|
export class ScreenshotHelper {
|
||
|
|
private screenshotQueue: string[] = []
|
||
|
|
private extraScreenshotQueue: string[] = []
|
||
|
|
private readonly MAX_SCREENSHOTS = 5
|
||
|
|
|
||
|
|
private readonly screenshotDir: string
|
||
|
|
private readonly extraScreenshotDir: string
|
||
|
|
|
||
|
|
private view: "queue" | "solutions" | "debug" = "queue"
|
||
|
|
|
||
|
|
constructor(view: "queue" | "solutions" | "debug" = "queue") {
|
||
|
|
this.view = view
|
||
|
|
|
||
|
|
// Initialize directories
|
||
|
|
this.screenshotDir = path.join(app.getPath("userData"), "screenshots")
|
||
|
|
this.extraScreenshotDir = path.join(
|
||
|
|
app.getPath("userData"),
|
||
|
|
"extra_screenshots"
|
||
|
|
)
|
||
|
|
|
||
|
|
// Create directories if they don't exist
|
||
|
|
if (!fs.existsSync(this.screenshotDir)) {
|
||
|
|
fs.mkdirSync(this.screenshotDir, { recursive: true })
|
||
|
|
}
|
||
|
|
if (!fs.existsSync(this.extraScreenshotDir)) {
|
||
|
|
fs.mkdirSync(this.extraScreenshotDir, { recursive: true })
|
||
|
|
}
|
||
|
|
|
||
|
|
// Ensure temp directory exists
|
||
|
|
const tempDir = app.getPath("temp")
|
||
|
|
if (!fs.existsSync(tempDir)) {
|
||
|
|
fs.mkdirSync(tempDir, { recursive: true })
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public getView(): "queue" | "solutions" | "debug" {
|
||
|
|
return this.view
|
||
|
|
}
|
||
|
|
|
||
|
|
public setView(view: "queue" | "solutions" | "debug"): void {
|
||
|
|
console.log("Setting view in ScreenshotHelper:", view)
|
||
|
|
console.log(
|
||
|
|
"Current queues - Main:",
|
||
|
|
this.screenshotQueue,
|
||
|
|
"Extra:",
|
||
|
|
this.extraScreenshotQueue
|
||
|
|
)
|
||
|
|
this.view = view
|
||
|
|
}
|
||
|
|
|
||
|
|
public getScreenshotQueue(): string[] {
|
||
|
|
return this.screenshotQueue
|
||
|
|
}
|
||
|
|
|
||
|
|
public getExtraScreenshotQueue(): string[] {
|
||
|
|
console.log("Getting extra screenshot queue:", this.extraScreenshotQueue)
|
||
|
|
return this.extraScreenshotQueue
|
||
|
|
}
|
||
|
|
|
||
|
|
public clearQueues(): void {
|
||
|
|
// Clear screenshotQueue
|
||
|
|
this.screenshotQueue.forEach((screenshotPath) => {
|
||
|
|
fs.unlink(screenshotPath, (err) => {
|
||
|
|
if (err)
|
||
|
|
console.error(`Error deleting screenshot at ${screenshotPath}:`, err)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
this.screenshotQueue = []
|
||
|
|
|
||
|
|
// Clear extraScreenshotQueue
|
||
|
|
this.extraScreenshotQueue.forEach((screenshotPath) => {
|
||
|
|
fs.unlink(screenshotPath, (err) => {
|
||
|
|
if (err)
|
||
|
|
console.error(
|
||
|
|
`Error deleting extra screenshot at ${screenshotPath}:`,
|
||
|
|
err
|
||
|
|
)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
this.extraScreenshotQueue = []
|
||
|
|
}
|
||
|
|
|
||
|
|
private async captureScreenshot(): Promise<Buffer> {
|
||
|
|
try {
|
||
|
|
// Ensure temp directory exists
|
||
|
|
const tempDir = app.getPath("temp")
|
||
|
|
if (!fs.existsSync(tempDir)) {
|
||
|
|
fs.mkdirSync(tempDir, { recursive: true })
|
||
|
|
}
|
||
|
|
|
||
|
|
// Use screenshot-desktop for cross-platform support
|
||
|
|
const buffer = await screenshot({ format: 'png' });
|
||
|
|
return buffer;
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error capturing screenshot:", error);
|
||
|
|
throw error;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async takeScreenshot(
|
||
|
|
hideMainWindow: () => void,
|
||
|
|
showMainWindow: () => void
|
||
|
|
): Promise<string> {
|
||
|
|
console.log("Taking screenshot in view:", this.view)
|
||
|
|
hideMainWindow()
|
||
|
|
await new Promise((resolve) => setTimeout(resolve, 100))
|
||
|
|
|
||
|
|
let screenshotPath = ""
|
||
|
|
try {
|
||
|
|
// Get screenshot buffer using cross-platform method
|
||
|
|
const screenshotBuffer = await this.captureScreenshot();
|
||
|
|
|
||
|
|
// Save and manage the screenshot based on current view
|
||
|
|
if (this.view === "queue") {
|
||
|
|
screenshotPath = path.join(this.screenshotDir, `${uuidv4()}.png`)
|
||
|
|
await fs.promises.writeFile(screenshotPath, screenshotBuffer)
|
||
|
|
console.log("Adding screenshot to main queue:", screenshotPath)
|
||
|
|
this.screenshotQueue.push(screenshotPath)
|
||
|
|
if (this.screenshotQueue.length > this.MAX_SCREENSHOTS) {
|
||
|
|
const removedPath = this.screenshotQueue.shift()
|
||
|
|
if (removedPath) {
|
||
|
|
try {
|
||
|
|
await fs.promises.unlink(removedPath)
|
||
|
|
console.log(
|
||
|
|
"Removed old screenshot from main queue:",
|
||
|
|
removedPath
|
||
|
|
)
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error removing old screenshot:", error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} else {
|
||
|
|
// In solutions view, only add to extra queue
|
||
|
|
screenshotPath = path.join(this.extraScreenshotDir, `${uuidv4()}.png`)
|
||
|
|
await fs.promises.writeFile(screenshotPath, screenshotBuffer)
|
||
|
|
console.log("Adding screenshot to extra queue:", screenshotPath)
|
||
|
|
this.extraScreenshotQueue.push(screenshotPath)
|
||
|
|
if (this.extraScreenshotQueue.length > this.MAX_SCREENSHOTS) {
|
||
|
|
const removedPath = this.extraScreenshotQueue.shift()
|
||
|
|
if (removedPath) {
|
||
|
|
try {
|
||
|
|
await fs.promises.unlink(removedPath)
|
||
|
|
console.log(
|
||
|
|
"Removed old screenshot from extra queue:",
|
||
|
|
removedPath
|
||
|
|
)
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error removing old screenshot:", error)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Screenshot error:", error)
|
||
|
|
throw error
|
||
|
|
} finally {
|
||
|
|
await new Promise((resolve) => setTimeout(resolve, 50))
|
||
|
|
showMainWindow()
|
||
|
|
}
|
||
|
|
|
||
|
|
return screenshotPath
|
||
|
|
}
|
||
|
|
|
||
|
|
public async getImagePreview(filepath: string): Promise<string> {
|
||
|
|
try {
|
||
|
|
const data = await fs.promises.readFile(filepath)
|
||
|
|
return `data:image/png;base64,${data.toString("base64")}`
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error reading image:", error)
|
||
|
|
throw error
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public async deleteScreenshot(
|
||
|
|
path: string
|
||
|
|
): Promise<{ success: boolean; error?: string }> {
|
||
|
|
try {
|
||
|
|
await fs.promises.unlink(path)
|
||
|
|
if (this.view === "queue") {
|
||
|
|
this.screenshotQueue = this.screenshotQueue.filter(
|
||
|
|
(filePath) => filePath !== path
|
||
|
|
)
|
||
|
|
} else {
|
||
|
|
this.extraScreenshotQueue = this.extraScreenshotQueue.filter(
|
||
|
|
(filePath) => filePath !== path
|
||
|
|
)
|
||
|
|
}
|
||
|
|
return { success: true }
|
||
|
|
} catch (error) {
|
||
|
|
console.error("Error deleting file:", error)
|
||
|
|
return { success: false, error: error.message }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public clearExtraScreenshotQueue(): void {
|
||
|
|
// Clear extraScreenshotQueue
|
||
|
|
this.extraScreenshotQueue.forEach((screenshotPath) => {
|
||
|
|
fs.unlink(screenshotPath, (err) => {
|
||
|
|
if (err)
|
||
|
|
console.error(
|
||
|
|
`Error deleting extra screenshot at ${screenshotPath}:`,
|
||
|
|
err
|
||
|
|
)
|
||
|
|
})
|
||
|
|
})
|
||
|
|
this.extraScreenshotQueue = []
|
||
|
|
}
|
||
|
|
}
|