@@ -3,101 +3,117 @@ import type { CarbonStar } from "../carbon-star";
33import type { ChmodOptions , FileInfo } from "./types" ;
44
55export class FileManager {
6- private star : CarbonStar ;
7- private axios : AxiosInstance ;
8-
9- constructor ( star : CarbonStar , axios : AxiosInstance ) {
10- this . star = star ;
11- this . axios = axios ;
12- }
13-
14- private async fetchFiles ( directory : string ) {
15- return this . axios . get < FileInfo [ ] > ( `/files?path=${ directory } ` ) . then ( res => res . data )
16- }
17-
18- async getFiles ( directory : string ) {
19- return await this . fetchFiles ( directory )
20- }
21-
22- async getFile ( path : string ) {
23- return this . axios . get < { content ?: string , imageSrc ?: string } > ( `/files/content?path=${ path } ` ) . then ( res => res . data )
24- }
25-
26- async saveFile ( path : string , content : string ) {
27- return this . axios . put ( "/files/write" , { path, content } )
28- }
29-
30- async chmod ( options : ChmodOptions ) {
31- return this . axios . post ( "/files/chmod" , options )
32- }
33-
34- async moveFiles ( path : string , files : { from : string , to : string } [ ] ) {
35- return this . axios . post ( "/files/move" , { path, files } )
36- }
37-
38- async renameFile ( path : string , name : string ) {
39- return this . axios . put ( "/files/rename" , { path, name } )
40- }
41-
42- async duplicateFile ( path : string ) {
43- return this . axios . post ( "/files/duplicate" , { path } )
44- }
45-
46- async downloadFile ( path : string ) {
47- return this . axios . get ( "/files/download" , { params : { path } , responseType : 'blob' } )
48- }
49-
50- async deleteFile ( params : { path ?: string , paths ?: string [ ] } ) {
51- const requestParams : { path ?: string , paths ?: string } = { } ;
52-
53- if ( params . path ) {
54- requestParams . path = params . path ;
55- }
56-
57- if ( params . paths ?. length ) {
58- requestParams . paths = `[${ params . paths . map ( path => `"${ path } "` ) . join ( "," ) } ]` ;
6+ private star : CarbonStar ;
7+ private axios : AxiosInstance ;
8+
9+ constructor ( star : CarbonStar , axios : AxiosInstance ) {
10+ this . star = star ;
11+ this . axios = axios ;
12+ }
13+
14+ private async fetchFiles ( directory : string ) {
15+ return this . axios
16+ . get < FileInfo [ ] > ( `/files?path=${ directory } ` )
17+ . then ( ( res ) => res . data ) ;
18+ }
19+
20+ async getFiles ( directory : string ) {
21+ return await this . fetchFiles ( directory ) ;
22+ }
23+
24+ async getFile ( path : string ) {
25+ return this . axios
26+ . get < { content ?: string ; imageSrc ?: string } > (
27+ `/files/content?path=${ path } ` ,
28+ )
29+ . then ( ( res ) => res . data ) ;
30+ }
31+
32+ async saveFile ( path : string , content : string ) {
33+ return this . axios . put ( "/files/write" , { path, content } ) ;
34+ }
35+
36+ async chmod ( options : ChmodOptions ) {
37+ return this . axios . post ( "/files/chmod" , options ) ;
38+ }
39+
40+ async moveFiles ( path : string , files : { from : string ; to : string } [ ] ) {
41+ return this . axios . post ( "/files/move" , { path, files } ) ;
42+ }
43+
44+ async renameFile ( path : string , name : string ) {
45+ return this . axios . put ( "/files/rename" , { path, name } ) ;
46+ }
47+
48+ async duplicateFile ( path : string ) {
49+ return this . axios . post ( "/files/duplicate" , { path } ) ;
50+ }
51+
52+ async downloadFile ( path : string ) {
53+ return this . axios . get ( "/files/download" , {
54+ params : { path } ,
55+ responseType : "blob" ,
56+ } ) ;
57+ }
58+
59+ async deleteFile ( params : { path ?: string ; paths ?: string [ ] } ) {
60+ const requestParams : { path ?: string ; paths ?: string } = { } ;
61+
62+ if ( params . path ) {
63+ requestParams . path = params . path ;
64+ }
65+
66+ if ( params . paths ?. length ) {
67+ requestParams . paths = `[${ params . paths . map ( ( path ) => `"${ path } "` ) . join ( "," ) } ]` ;
68+ }
69+
70+ return this . axios . delete ( "/files" , { params : requestParams } ) ;
71+ }
72+
73+ async createFile ( parentDirectory : string , fileName : string ) {
74+ return this . axios . post ( "/files" , {
75+ type : "file" ,
76+ path : `${ parentDirectory } /${ fileName } ` ,
77+ } ) ;
78+ }
79+
80+ async createDirectory ( parentDirectory : string , folderName : string ) {
81+ return this . axios . post ( "/files" , {
82+ type : "directory" ,
83+ path : `${ parentDirectory } /${ folderName } ` ,
84+ } ) ;
85+ }
86+
87+ async decompressFile ( root : string , file : string ) {
88+ return this . axios . post ( "/files/decompress" , { root, file } ) ;
89+ }
90+
91+ async compressFiles ( root : string , files : string [ ] ) {
92+ return this . axios . post ( "/files/compress" , { root, files } ) ;
93+ }
94+
95+ async uploadFile ( path : string , file : File ) {
96+ try {
97+ // get url
98+ const url = await this . axios
99+ . get ( "/files/upload" , { params : { path } } )
100+ . then ( ( res ) => res . data . url ) ;
101+
102+ const formData = new FormData ( ) ;
103+ formData . append ( "files" , file ) ;
104+
105+ const updatedURL = `${ url } &directory=${ path } ` ;
106+
107+ // Send the FormData directly, not wrapped in an object
108+ return await this . axios . post ( updatedURL , formData , {
109+ headers : {
110+ // Don't set Content-Type manually - axios will set it automatically with boundary
111+ 'Content-Type' : 'multipart/form-data'
59112 }
60-
61- return this . axios . delete ( "/files" , { params : requestParams } )
62- }
63-
64- async createFile ( parentDirectory : string , fileName : string ) {
65- return this . axios . post ( "/files" , {
66- type : "file" ,
67- path : `${ parentDirectory } /${ fileName } `
68- } )
69- }
70-
71- async createDirectory ( parentDirectory : string , folderName : string ) {
72- return this . axios . post ( "/files" , {
73- type : "directory" ,
74- path : `${ parentDirectory } /${ folderName } `
75- } )
76- }
77-
78- async decompressFile ( root : string , file : string ) {
79- return this . axios . post ( "/files/decompress" , { root, file } )
80- }
81-
82- async compressFiles ( root : string , files : string [ ] ) {
83- return this . axios . post ( "/files/compress" , { root, files } )
84- }
85-
86- async uploadFile ( path : string , file : File ) {
87-
88- // get url
89- const url = await this . axios . get ( "/files/upload" , { params : { path } } )
90- . then ( res => res . data . url )
91-
92-
93- // upload file
94- // files form data
95- // directory
96- const formData = new FormData ( ) ;
97- formData . append ( "files" , file ) ;
98-
99- const updatedURL = `${ url } ?directory=${ path } ` ;
100-
101- return this . axios . post ( updatedURL , formData )
113+ } ) ;
114+ } catch ( err ) {
115+ console . error ( 'Upload error:' , err ) ;
116+ throw err ;
102117 }
103- }
118+ }
119+ }
0 commit comments