From 3d5041de7cc7e72b88ad4a2341540a8877cb479b Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 17 Dec 2019 10:01:09 +0100 Subject: [PATCH] MOBILE-3213 ios: Add timestamp to some files in iOS --- .../fileuploader/providers/fileuploader.ts | 17 ++++++++----- src/core/fileuploader/providers/helper.ts | 25 +++++++++++-------- 2 files changed, 25 insertions(+), 17 deletions(-) diff --git a/src/core/fileuploader/providers/fileuploader.ts b/src/core/fileuploader/providers/fileuploader.ts index 520e5e631..a061b8e29 100644 --- a/src/core/fileuploader/providers/fileuploader.ts +++ b/src/core/fileuploader/providers/fileuploader.ts @@ -150,20 +150,25 @@ export class CoreFileUploaderProvider { * @return Options. */ getCameraUploadOptions(uri: string, isFromAlbum?: boolean): CoreFileUploaderOptions { - const extension = this.mimeUtils.getExtension(uri), - mimetype = this.mimeUtils.getMimeType(extension), - isIOS = this.platform.is('ios'), - options: CoreFileUploaderOptions = { + const extension = this.mimeUtils.guessExtensionFromUrl(uri); + const mimetype = this.mimeUtils.getMimeType(extension); + const isIOS = this.platform.is('ios'); + const options: CoreFileUploaderOptions = { deleteAfterUpload: !isFromAlbum, mimeType: mimetype }; + const fileName = this.fileProvider.getFileAndDirectoryFromPath(uri).name; if (isIOS && (mimetype == 'image/jpeg' || mimetype == 'image/png')) { // In iOS, the pictures can have repeated names, even if they come from the album. - options.fileName = 'image_' + this.timeUtils.readableTimestamp() + '.' + extension; + // Add a timestamp to the filename to make it unique. + const split = fileName.split('.'); + split[0] += '_' + this.timeUtils.readableTimestamp(); + + options.fileName = split.join('.'); } else { // Use the same name that the file already has. - options.fileName = this.fileProvider.getFileAndDirectoryFromPath(uri).name; + options.fileName = fileName; } if (isFromAlbum) { diff --git a/src/core/fileuploader/providers/helper.ts b/src/core/fileuploader/providers/helper.ts index b812f8014..fb1ac27a0 100644 --- a/src/core/fileuploader/providers/helper.ts +++ b/src/core/fileuploader/providers/helper.ts @@ -128,10 +128,12 @@ export class CoreFileUploaderHelperProvider { * @param defaultExt Defaut extension to use if the file doesn't have any. * @return Promise resolved with the copied file. */ - protected copyToTmpFolder(path: string, shouldDelete: boolean, maxSize?: number, defaultExt?: string): Promise { - let fileName = this.fileProvider.getFileAndDirectoryFromPath(path).name, - promise, - fileTooLarge; + protected copyToTmpFolder(path: string, shouldDelete: boolean, maxSize?: number, defaultExt?: string, + options?: CoreFileUploaderOptions): Promise { + + const fileName = (options && options.fileName) || this.fileProvider.getFileAndDirectoryFromPath(path).name; + let promise; + let fileTooLarge; // Check that size isn't too large. if (typeof maxSize != 'undefined' && maxSize != -1) { @@ -154,9 +156,6 @@ export class CoreFileUploaderHelperProvider { } // File isn't too large. - // Picking an image from album in Android adds a timestamp at the end of the file. Delete it. - fileName = fileName.replace(/(\.[^\.]*)\?[^\.]*$/, '$1'); - // Get a unique name in the folder to prevent overriding another file. return this.fileProvider.getUniqueNameInFolder(CoreFileProvider.TMPFOLDER, fileName, defaultExt); }).then((newName) => { @@ -468,11 +467,13 @@ export class CoreFileUploaderHelperProvider { path = 'file://' + path; } + const options = this.fileUploaderProvider.getMediaUploadOptions(media); + if (upload) { - return this.uploadFile(path, maxSize, true, this.fileUploaderProvider.getMediaUploadOptions(media)); + return this.uploadFile(path, maxSize, true, options); } else { // Copy or move the file to our temporary folder. - return this.copyToTmpFolder(path, true, maxSize); + return this.copyToTmpFolder(path, true, maxSize, undefined, options); } }, (error) => { const defaultError = isAudio ? 'core.fileuploader.errorcapturingaudio' : 'core.fileuploader.errorcapturingvideo'; @@ -552,11 +553,13 @@ export class CoreFileUploaderHelperProvider { return Promise.reject(error); } + const options = this.fileUploaderProvider.getCameraUploadOptions(path, fromAlbum); + if (upload) { - return this.uploadFile(path, maxSize, true, this.fileUploaderProvider.getCameraUploadOptions(path, fromAlbum)); + return this.uploadFile(path, maxSize, true, options); } else { // Copy or move the file to our temporary folder. - return this.copyToTmpFolder(path, !fromAlbum, maxSize, 'jpg'); + return this.copyToTmpFolder(path, !fromAlbum, maxSize, 'jpg', options); } }, (error) => { const defaultError = fromAlbum ? 'core.fileuploader.errorgettingimagealbum' : 'core.fileuploader.errorcapturingimage';