@@ -29,7 +29,7 @@ export default class Files {
2929 query . overwrite = overwrite
3030 }
3131
32- filePath = FilesUtils . preventSlashInPath ( filePath )
32+ filePath = FilesUtils . trimSlashesInPath ( filePath )
3333 fileName = FilesUtils . sanitizeFileName ( fileName )
3434
3535 return this . app . request . put ( {
@@ -47,7 +47,7 @@ export default class Files {
4747 query . overwrite = overwrite
4848 }
4949
50- filePath = FilesUtils . preventSlashInPath ( filePath )
50+ filePath = FilesUtils . trimSlashesInPath ( filePath )
5151
5252 const pathTokens = FilesUtils . parseFilePath ( filePath )
5353
@@ -66,7 +66,7 @@ export default class Files {
6666 }
6767
6868 return this . app . request . post ( {
69- url : `${ this . app . urls . filePath ( filePath ) } /${ fileName || '' } ` ,
69+ url : `${ this . app . urls . filePath ( filePath ) } /${ fileName } ` ,
7070 query : query ,
7171 data : {
7272 url : file
@@ -93,14 +93,102 @@ export default class Files {
9393 } )
9494 }
9595
96+ async append ( filePath , fileName , fileContent ) {
97+ if ( ! filePath || typeof filePath !== 'string' ) {
98+ throw new Error ( '"filePath" must be provided and must be a string.' )
99+ }
100+
101+ filePath = FilesUtils . trimSlashesInPath ( filePath )
102+
103+ if ( arguments . length === 2 ) {
104+ fileContent = fileName
105+ fileName = undefined
106+
107+ const pathTokens = FilesUtils . parseFilePath ( filePath )
108+
109+ if ( pathTokens . fileName ) {
110+ filePath = pathTokens . filePath
111+ fileName = pathTokens . fileName
112+ }
113+ }
114+
115+ if ( ! fileName ) {
116+ throw new Error ( 'Can not resolve target file name' )
117+ }
118+
119+ fileName = FilesUtils . sanitizeFileName ( fileName )
120+
121+ if ( typeof fileContent === 'string' ) {
122+ return this . app . request . post ( {
123+ url : `${ this . app . urls . fileAppendPath ( filePath ) } /${ fileName } ` ,
124+ data : {
125+ url : fileContent
126+ } ,
127+ } )
128+ }
129+
130+ if ( FilesUtils . isBytesArray ( fileContent ) ) {
131+ fileContent = await FilesUtils . toBase64 ( fileContent )
132+
133+ return this . app . request . put ( {
134+ url : `${ this . app . urls . fileAppendBinaryPath ( filePath ) } /${ fileName } ` ,
135+ headers : { 'Content-Type' : 'text/plain' } ,
136+ data : fileContent ,
137+ } )
138+ }
139+
140+ return this . app . request . post ( {
141+ url : `${ this . app . urls . fileAppendPath ( filePath ) } /${ fileName } ` ,
142+ form : {
143+ file : fileContent
144+ } ,
145+ } )
146+ }
147+
148+ async appendText ( filePath , fileName , textContent ) {
149+ if ( ! filePath || typeof filePath !== 'string' ) {
150+ throw new Error ( '"filePath" must be provided and must be a string.' )
151+ }
152+
153+ filePath = FilesUtils . trimSlashesInPath ( filePath )
154+
155+ if ( arguments . length === 2 ) {
156+ textContent = fileName
157+ fileName = undefined
158+
159+ const pathTokens = FilesUtils . parseFilePath ( filePath )
160+
161+ if ( pathTokens . fileName ) {
162+ filePath = pathTokens . filePath
163+ fileName = pathTokens . fileName
164+ }
165+ }
166+
167+ if ( ! fileName ) {
168+ throw new Error ( 'Can not resolve target file name' )
169+ }
170+
171+ if ( typeof textContent !== 'string' ) {
172+ throw new Error ( '"textContent" must be a string' )
173+ }
174+
175+ fileName = FilesUtils . sanitizeFileName ( fileName )
176+
177+ return this . app . request . put ( {
178+ url : `${ this . app . urls . fileAppendPath ( filePath ) } /${ fileName } ` ,
179+ headers : { 'Content-Type' : 'text/plain' } ,
180+ data : textContent ,
181+ } )
182+ }
183+
96184 async listing ( filePath , pattern , sub , pagesize , offset ) {
97185 const query = { }
98186
99187 if ( ! filePath || typeof filePath !== 'string' ) {
100188 throw new Error ( '"filePath" must be provided and must be a string.' )
101189 }
102190
103- filePath = FilesUtils . preventSlashInPath ( filePath )
191+ filePath = FilesUtils . trimSlashesInPath ( filePath )
104192
105193 if ( pattern && typeof pattern === 'string' ) {
106194 query . pattern = pattern
@@ -181,7 +269,7 @@ export default class Files {
181269 throw new Error ( '"filePath" must be provided and must be a string.' )
182270 }
183271
184- filePath = FilesUtils . preventSlashInPath ( filePath )
272+ filePath = FilesUtils . trimSlashesInPath ( filePath )
185273
186274 return this . app . request . get ( {
187275 url : this . app . urls . filePath ( filePath ) ,
@@ -196,7 +284,7 @@ export default class Files {
196284 throw new Error ( 'Directory "path" must be provided and must be a string.' )
197285 }
198286
199- directoryPath = FilesUtils . preventSlashInPath ( directoryPath )
287+ directoryPath = FilesUtils . trimSlashesInPath ( directoryPath )
200288
201289 return this . app . request . delete ( {
202290 url : this . app . urls . filePath ( directoryPath ) ,
@@ -212,7 +300,7 @@ export default class Files {
212300 throw new Error ( 'Files Pattern must be provided and must be a string.' )
213301 }
214302
215- filesPath = FilesUtils . preventSlashInPath ( filesPath )
303+ filesPath = FilesUtils . trimSlashesInPath ( filesPath )
216304
217305 return this . app . request . get ( {
218306 url : this . app . urls . filePath ( filesPath ) ,
0 commit comments