11import { execFileSync , spawnSync } from 'node:child_process' ;
2+ import { randomUUID } from 'node:crypto' ;
23import { existsSync , mkdirSync , rmSync , writeFileSync } from 'node:fs' ;
34import { tmpdir } from 'node:os' ;
45import { join } from 'node:path' ;
@@ -12,12 +13,9 @@ function ensureTempDirectory(directory: string): string {
1213 return directory ;
1314}
1415
15- function buildTargetPath (
16- directory : string ,
17- baseName : string ,
18- extension : string ,
19- ) {
20- return join ( ensureTempDirectory ( directory ) , `${ baseName } .${ extension } ` ) ;
16+ function buildTargetPath ( directory : string , extension : string ) {
17+ const uniqueName = `${ randomUUID ( ) } .${ extension } ` ;
18+ return join ( ensureTempDirectory ( directory ) , uniqueName ) ;
2119}
2220
2321function readMacClipboardImage ( path : string ) : void {
@@ -64,13 +62,13 @@ $image.Save($args[0], [System.Drawing.Imaging.ImageFormat]::Png)
6462 } ) ;
6563}
6664
67- function readLinuxClipboardImage ( directory : string , baseName : string ) : string {
65+ function readLinuxClipboardImage ( directory : string ) : string {
6866 const wlPng = spawnSync ( 'wl-paste' , [ '--no-newline' , '--type' , 'image/png' ] , {
6967 encoding : 'buffer' ,
7068 } ) ;
7169 if ( wlPng . status === 0 && wlPng . stdout . length > 0 ) {
72- const path = buildTargetPath ( directory , baseName , 'png' ) ;
73- writeFileSync ( path , wlPng . stdout ) ;
70+ const path = buildTargetPath ( directory , 'png' ) ;
71+ writeClipboardImageFile ( path , wlPng . stdout ) ;
7472 return path ;
7573 }
7674
@@ -80,8 +78,8 @@ function readLinuxClipboardImage(directory: string, baseName: string): string {
8078 { encoding : 'buffer' } ,
8179 ) ;
8280 if ( xclipPng . status === 0 && xclipPng . stdout . length > 0 ) {
83- const path = buildTargetPath ( directory , baseName , 'png' ) ;
84- writeFileSync ( path , xclipPng . stdout ) ;
81+ const path = buildTargetPath ( directory , 'png' ) ;
82+ writeClipboardImageFile ( path , xclipPng . stdout ) ;
8583 return path ;
8684 }
8785
@@ -90,24 +88,31 @@ function readLinuxClipboardImage(directory: string, baseName: string): string {
9088 ) ;
9189}
9290
91+ function writeClipboardImageFile ( path : string , data : Buffer ) : void {
92+ writeFileSync ( path , data , { flag : 'wx' , mode : 0o600 } ) ;
93+ }
94+
9395export function saveClipboardImage (
9496 baseName : string ,
9597 directory = TEMP_IMAGES_DIRECTORY ,
9698) : string {
9799 try {
98100 switch ( process . platform ) {
99101 case 'darwin' : {
100- const path = buildTargetPath ( directory , baseName , 'png' ) ;
102+ const path = buildTargetPath ( directory , 'png' ) ;
101103 readMacClipboardImage ( path ) ;
102104 return path ;
103105 }
106+
104107 case 'win32' : {
105- const path = buildTargetPath ( directory , baseName , 'png' ) ;
108+ const path = buildTargetPath ( directory , 'png' ) ;
106109 readWindowsClipboardImage ( path ) ;
107110 return path ;
108111 }
112+
109113 case 'linux' :
110- return readLinuxClipboardImage ( directory , baseName ) ;
114+ return readLinuxClipboardImage ( directory ) ;
115+
111116 default :
112117 throw new Error (
113118 'Clipboard image paste is not supported on this platform. Paste an image path instead.' ,
0 commit comments