Skip to content

Commit c7c5659

Browse files
committed
MOBILE-1982 upload: Use real name in album files
1 parent 01b76f6 commit c7c5659

File tree

2 files changed

+38
-7
lines changed

2 files changed

+38
-7
lines changed

www/core/components/fileuploader/services/fileuploader.js

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
angular.module('mm.core.fileuploader')
1616

17-
.factory('$mmFileUploader', function($mmSite, $mmFS, $q, $timeout, $log, $mmSitesManager, $mmFilepool) {
17+
.factory('$mmFileUploader', function($mmSite, $mmFS, $q, $timeout, $log, $mmSitesManager, $mmFilepool, $mmUtil) {
1818

1919
$log = $log.getInstance('$mmFileUploader');
2020

@@ -124,19 +124,38 @@ angular.module('mm.core.fileuploader')
124124
*/
125125
self.uploadImage = function(uri, isFromAlbum) {
126126
$log.debug('Uploading an image');
127-
var options = {};
128-
129-
if (typeof uri == 'undefined' || uri === ''){
127+
var options = {
128+
fileName: 'image_' + $mmUtil.readableTimestamp() + '.jpg', // Default file name.
129+
mimeType: 'image/jpeg'
130+
},
131+
fileName,
132+
extension;
133+
134+
if (typeof uri == 'undefined' || uri === '') {
130135
// In Node-Webkit, if you successfully upload a picture and then you open the file picker again
131136
// and cancel, this function is called with an empty uri. Let's filter it.
132137
$log.debug('Received invalid URI in $mmFileUploader.uploadImage()');
133138
return $q.reject();
134139
}
135140

141+
// Check if we know the real file name.
142+
if (isFromAlbum) {
143+
fileName = $mmFS.getFileAndDirectoryFromPath(uri).name;
144+
// Picking an image from album in Android adds a timestamp at the end of the file. Delete it.
145+
fileName = fileName.replace(/(\.[^\.]*)\?[^\.]*$/, '$1');
146+
147+
extension = $mmFS.getFileExtension(fileName);
148+
149+
if (extension) {
150+
// The file has extension, use the real name.
151+
options.fileName = fileName;
152+
options.mimeType = $mmFS.getMimeType(extension);
153+
}
154+
}
155+
156+
136157
options.deleteAfterUpload = !isFromAlbum;
137158
options.fileKey = 'file';
138-
options.fileName = 'image_' + new Date().getTime() + '.jpg';
139-
options.mimeType = 'image/jpeg';
140159

141160
return self.uploadFile(uri, options);
142161
};
@@ -158,7 +177,7 @@ angular.module('mm.core.fileuploader')
158177

159178
// Add a timestamp to the filename to make it unique.
160179
split = filename.split('.');
161-
split[0] += '_' + new Date().getTime();
180+
split[0] += '_' + $mmUtil.readableTimestamp();
162181
filename = split.join('.');
163182

164183
options.fileKey = null;

www/core/lib/util.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,18 @@ angular.module('mm.core')
915915
return Math.round(Date.now() / 1000);
916916
};
917917

918+
/**
919+
* Return the current timestamp in a "readable" format: YYYYMMDDHHmmSS.
920+
*
921+
* @module mm.core
922+
* @ngdoc method
923+
* @name $mmUtil#readableTimestamp
924+
* @return {Number} The readable timestamp.
925+
*/
926+
self.readableTimestamp = function() {
927+
return moment(Date.now()).format('YYYYMMDDHHmmSS');
928+
};
929+
918930
/**
919931
* Return true if the param is false (bool), 0 (number) or "0" (string).
920932
*

0 commit comments

Comments
 (0)