File tree Expand file tree Collapse file tree 1 file changed +11
-10
lines changed
application/holder/src/service/electron Expand file tree Collapse file tree 1 file changed +11
-10
lines changed Original file line number Diff line number Diff line change @@ -5,18 +5,19 @@ import * as fs from 'fs';
55
66export class Protocol {
77 public register ( ) {
8- protocol . registerFileProtocol ( 'attachment' , this . attachment . bind ( this ) ) ;
8+ protocol . handle ( 'attachment' , this . attachment . bind ( this ) ) ;
99 }
1010
11- protected attachment (
12- request : Electron . ProtocolRequest ,
13- callback : ( response : string | Electron . ProtocolResponse ) => void ,
14- ) : void {
15- const path = request . url . slice ( 'attachment://' . length ) ;
16- if ( fs . existsSync ( path ) ) {
17- callback ( url . fileURLToPath ( `file://${ path } ` ) ) ;
18- } else {
19- callback ( 'attachment://file_does_not_exist' ) ;
11+ protected async attachment ( request : Request ) : Promise < Response > {
12+ const path = url . fileURLToPath ( new URL ( request . url . replace ( / ^ a t t a c h m e n t : / , 'file:' ) ) ) ;
13+ if ( ! fs . existsSync ( path ) ) {
14+ return Promise . reject ( new Error ( `File doesn't exist` ) ) ;
15+ }
16+ try {
17+ const buffer = await fs . promises . readFile ( path ) ;
18+ return new Response ( buffer ) ;
19+ } catch ( _err ) {
20+ return new Response ( 'File not found' , { status : 404 } ) ;
2021 }
2122 }
2223}
You can’t perform that action at this time.
0 commit comments