pi-image-tools 1.0.4 → 1.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/temp-file.ts CHANGED
@@ -1,82 +1,82 @@
1
- import { randomUUID } from "node:crypto";
2
- import { mkdirSync, readdirSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
3
- import { tmpdir } from "node:os";
4
- import { join } from "node:path";
5
-
6
- import type { ClipboardImage } from "./types.js";
7
-
8
- function extensionForImageMimeType(mimeType: string): string {
9
- const normalized = mimeType.split(";")[0]?.trim().toLowerCase() ?? mimeType.toLowerCase();
10
-
11
- switch (normalized) {
12
- case "image/png":
13
- return "png";
14
- case "image/jpeg":
15
- return "jpg";
16
- case "image/webp":
17
- return "webp";
18
- case "image/gif":
19
- return "gif";
20
- default:
21
- return "png";
22
- }
23
- }
24
-
25
- export class TempFileManager {
26
- private readonly baseDir: string;
27
- private readonly createdFiles = new Set<string>();
28
- private exitHookRegistered = false;
29
-
30
- constructor(baseDir: string = join(tmpdir(), "pi-images")) {
31
- this.baseDir = baseDir;
32
- }
33
-
34
- registerExitCleanup(): void {
35
- if (this.exitHookRegistered) {
36
- return;
37
- }
38
-
39
- process.once("exit", () => {
40
- this.cleanupSync();
41
- });
42
-
43
- this.exitHookRegistered = true;
44
- }
45
-
46
- saveImage(image: ClipboardImage): string {
47
- mkdirSync(this.baseDir, { recursive: true });
48
-
49
- const ext = extensionForImageMimeType(image.mimeType);
50
- const filePath = join(this.baseDir, `pi-image-${Date.now()}-${randomUUID()}.${ext}`);
51
-
52
- writeFileSync(filePath, Buffer.from(image.bytes));
53
- this.createdFiles.add(filePath);
54
-
55
- return filePath;
56
- }
57
-
58
- async cleanup(): Promise<void> {
59
- this.cleanupSync();
60
- }
61
-
62
- cleanupSync(): void {
63
- for (const filePath of this.createdFiles) {
64
- try {
65
- unlinkSync(filePath);
66
- } catch {
67
- // Best-effort cleanup only.
68
- }
69
- }
70
-
71
- this.createdFiles.clear();
72
-
73
- try {
74
- const entries = readdirSync(this.baseDir);
75
- if (entries.length === 0) {
76
- rmSync(this.baseDir, { recursive: true, force: true });
77
- }
78
- } catch {
79
- // Ignore directory cleanup errors.
80
- }
81
- }
82
- }
1
+ import { randomUUID } from "node:crypto";
2
+ import { mkdirSync, readdirSync, rmSync, unlinkSync, writeFileSync } from "node:fs";
3
+ import { tmpdir } from "node:os";
4
+ import { join } from "node:path";
5
+
6
+ import type { ClipboardImage } from "./types.js";
7
+
8
+ function extensionForImageMimeType(mimeType: string): string {
9
+ const normalized = mimeType.split(";")[0]?.trim().toLowerCase() ?? mimeType.toLowerCase();
10
+
11
+ switch (normalized) {
12
+ case "image/png":
13
+ return "png";
14
+ case "image/jpeg":
15
+ return "jpg";
16
+ case "image/webp":
17
+ return "webp";
18
+ case "image/gif":
19
+ return "gif";
20
+ default:
21
+ return "png";
22
+ }
23
+ }
24
+
25
+ export class TempFileManager {
26
+ private readonly baseDir: string;
27
+ private readonly createdFiles = new Set<string>();
28
+ private exitHookRegistered = false;
29
+
30
+ constructor(baseDir: string = join(tmpdir(), "pi-images")) {
31
+ this.baseDir = baseDir;
32
+ }
33
+
34
+ registerExitCleanup(): void {
35
+ if (this.exitHookRegistered) {
36
+ return;
37
+ }
38
+
39
+ process.once("exit", () => {
40
+ this.cleanupSync();
41
+ });
42
+
43
+ this.exitHookRegistered = true;
44
+ }
45
+
46
+ saveImage(image: ClipboardImage): string {
47
+ mkdirSync(this.baseDir, { recursive: true });
48
+
49
+ const ext = extensionForImageMimeType(image.mimeType);
50
+ const filePath = join(this.baseDir, `pi-image-${Date.now()}-${randomUUID()}.${ext}`);
51
+
52
+ writeFileSync(filePath, Buffer.from(image.bytes));
53
+ this.createdFiles.add(filePath);
54
+
55
+ return filePath;
56
+ }
57
+
58
+ async cleanup(): Promise<void> {
59
+ this.cleanupSync();
60
+ }
61
+
62
+ cleanupSync(): void {
63
+ for (const filePath of this.createdFiles) {
64
+ try {
65
+ unlinkSync(filePath);
66
+ } catch {
67
+ // Best-effort cleanup only.
68
+ }
69
+ }
70
+
71
+ this.createdFiles.clear();
72
+
73
+ try {
74
+ const entries = readdirSync(this.baseDir);
75
+ if (entries.length === 0) {
76
+ rmSync(this.baseDir, { recursive: true, force: true });
77
+ }
78
+ } catch {
79
+ // Ignore directory cleanup errors.
80
+ }
81
+ }
82
+ }