@@ -264,6 +264,77 @@ export async function deleteObjects(
264264 return Private . deleteSingleObject ( driveName , options . path ) ;
265265}
266266
267+ /**
268+ * Rename an object.
269+ *
270+ * @param driveName
271+ * @param options.path The path of object.
272+ *
273+ * @returns A promise which resolves with the contents model.
274+ */
275+ export async function renameObjects (
276+ driveName : string ,
277+ options : {
278+ path : string ;
279+ newPath : string ;
280+ newFileName : string ;
281+ registeredFileTypes : IRegisteredFileTypes ;
282+ }
283+ ) {
284+ const formattedNewPath =
285+ options . newPath . substring ( 0 , options . newPath . lastIndexOf ( '/' ) + 1 ) +
286+ options . newFileName ;
287+
288+ const [ fileType , fileMimeType , fileFormat ] = getFileType (
289+ PathExt . extname ( PathExt . basename ( options . newFileName ) ) ,
290+ options . registeredFileTypes
291+ ) ;
292+
293+ // get list of contents with given prefix (path)
294+ const response = await requestAPI < any > (
295+ 'drives/' + driveName + '/' + options . path ,
296+ 'GET'
297+ ) ;
298+
299+ // renaming contents of a directory
300+ if ( response . data . length !== undefined && response . data . length !== 0 ) {
301+ await Promise . all (
302+ response . data . map ( async ( c : any ) => {
303+ const remainingFilePath = c . path . substring ( options . path . length ) ;
304+ Private . renameSingleObject (
305+ driveName ,
306+ PathExt . join ( options . path , remainingFilePath ) ,
307+ PathExt . join ( formattedNewPath , remainingFilePath )
308+ ) ;
309+ } )
310+ ) ;
311+ }
312+ // always rename the object (file or main directory)
313+ try {
314+ const renamedObject = await Private . renameSingleObject (
315+ driveName ,
316+ options . path ,
317+ formattedNewPath
318+ ) ;
319+ data = {
320+ name : options . newFileName ,
321+ path : PathExt . join ( driveName , formattedNewPath ) ,
322+ last_modified : renamedObject . data . last_modified ,
323+ created : '' ,
324+ content : PathExt . extname ( options . newFileName ) !== '' ? null : [ ] , // TODO: add dir check
325+ format : fileFormat as Contents . FileFormat ,
326+ mimetype : fileMimeType ,
327+ size : renamedObject . data . size ,
328+ writable : true ,
329+ type : fileType
330+ } ;
331+ } catch ( error ) {
332+ // renaming failed if directory didn't exist and was only part of a path
333+ }
334+
335+ return data ;
336+ }
337+
267338/**
268339 * Check existance of an object.
269340 *
@@ -335,4 +406,25 @@ namespace Private {
335406 ) {
336407 await requestAPI < any > ( 'drives/' + driveName + '/' + objectPath , 'DELETE' ) ;
337408 }
409+
410+ /**
411+ * Helping function for renaming files inside
412+ * a directory, in the case of deleting the directory.
413+ *
414+ * @param driveName
415+ * @param objectPath complete path of object to delete
416+ */
417+ export async function renameSingleObject (
418+ driveName : string ,
419+ objectPath : string ,
420+ newObjectPath : string
421+ ) {
422+ return await requestAPI < any > (
423+ 'drives/' + driveName + '/' + objectPath ,
424+ 'PATCH' ,
425+ {
426+ new_path : newObjectPath
427+ }
428+ ) ;
429+ }
338430}
0 commit comments