Chromium Code Reviews| Index: ui/file_manager/file_manager/background/js/media_import_handler.js |
| diff --git a/ui/file_manager/file_manager/background/js/media_import_handler.js b/ui/file_manager/file_manager/background/js/media_import_handler.js |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..3f9afee6c451494be1400c0ccac5236f9d2e6733 |
| --- /dev/null |
| +++ b/ui/file_manager/file_manager/background/js/media_import_handler.js |
| @@ -0,0 +1,162 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +/** |
| + * Handler for importing media from removable devices into the user's Drive. |
| + * @param {!FileOperationManager} fileOperationManager |
| + * @param {!MediaScanner=} opt_scanner |
| + * @constructor |
| + * @struct |
| + */ |
| +function MediaImportHandler(fileOperationManager, opt_scanner) { |
| + /** |
| + * Progress center to submit the progressing item. |
| + * @private {!FileOperationManager} |
| + * @const |
| + */ |
| + this.fileOperationManager_ = fileOperationManager; |
| + |
| + /** @private {!MediaScanner} */ |
| + this.scanner_ = opt_scanner || new MediaScanner(); |
| +} |
| + |
| +/** |
| + * @const {string} The name of the destination directory for media imports. |
| + */ |
| +MediaImportHandler.DESTINATION = 'photos'; |
| + |
| +/** |
| + * Import all media found in a given subdirectory tree. |
| + * @param {!DirectoryEntry} directory The directory to import media from. |
| + * @return {!MediaImportHandler.ImportTask} The resulting import task. |
| + */ |
| +MediaImportHandler.prototype.importFrom = function(directory) { |
| + return new MediaImportHandler.ImportTask( |
| + this.fileOperationManager_, |
| + this.scanner_, |
| + directory); |
| +}; |
| + |
| +/** |
| + * Retrieves the user's drive root. |
| + * @return {!Promise<!DirectoryEntry>} |
|
hirono
2014/12/04 04:44:26
!Promise.<!DirectoryEntry>
Ben Kwa
2014/12/04 19:53:02
Acknowledged.
|
| + * @private |
| + */ |
| +MediaImportHandler.getDriveRoot_ = function() { |
| + // TODO(kenobi): change this to 'other' to get the unparented drive root. |
| + /** @const {string} */ |
| + var rootName = 'root'; |
| + |
| + return VolumeManager.getInstance() |
| + .then( |
| + function(volumeManager) { |
| + var drive = volumeManager.getCurrentProfileVolumeInfo( |
| + VolumeManagerCommon.VolumeType.DRIVE); |
| + // Use the root for unparented objects. |
| + return new Promise(function(resolve, reject) { |
| + drive.fileSystem.root.getDirectory( |
|
hirono
2014/12/04 04:44:26
Does it work?
IIUC, drive.fileSystem.root just has
Ben Kwa
2014/12/04 19:53:02
Yup, this works - it's basically just getting the
|
| + rootName, |
| + {create: false}, |
| + resolve, |
| + reject); |
| + }); |
| + }); |
| +}; |
| + |
| +/** |
| + * Fetches (creating if necessary) the import destination subdirectory. |
| + * @param {!DirectoryEntry} root The drive root. |
| + * @return {!Promise<!DirectoryEntry>} |
|
hirono
2014/12/04 04:44:25
!Promise.<!DirectoryEntry>
Ben Kwa
2014/12/04 19:53:02
Acknowledged.
|
| + * @private |
| + */ |
| +MediaImportHandler.getOrCreateImportDestination_ = function(root) { |
| + /** |
| + * @param {string} name The name of the new directory. |
| + * @param {!DirectoryEntry} entry The parent directory. |
| + * @return {!Promise<!DirectoryEntry>} The created directory. |
|
hirono
2014/12/04 04:44:25
!Promise.<!DirectoryEntry>
Ben Kwa
2014/12/04 19:53:02
Acknowledged.
|
| + */ |
| + var mkdir_ = function(name, entry) { |
| + /** @const {Object} */ |
| + var CREATE_OPTIONS = { |
| + create: true, |
| + exclusive: false |
| + }; |
| + return new Promise(function(resolve, reject) { |
| + entry.getDirectory(name, CREATE_OPTIONS, resolve, reject); |
| + }); |
| + }; |
| + |
| + /** |
| + * @return {string} The current date, in YYYY-MM-DD format. |
| + */ |
| + var getDateString = function() { |
| + var padAndConvert = function(i) { |
| + return (i < 10 ? '0' : '') + i.toString(); |
| + }; |
| + var date = new Date(); |
| + var year = date.getFullYear().toString(); |
| + var month = padAndConvert(date.getMonth()); |
| + var day = padAndConvert(date.getDate()); |
| + |
| + return year + '-' + month + '-' + day; |
| + }; |
| + |
| + return Promise.resolve(root) |
| + .then(mkdir_.bind(this, MediaImportHandler.DESTINATION)) |
| + .then(mkdir_.bind(this, getDateString())) |
| + .catch(function(e) { console.error(e); }); |
|
hirono
2014/12/04 04:44:26
It turns rejected promise to fulfilled promise. Th
Ben Kwa
2014/12/04 19:53:02
Oops, this was debug code that I meant to remove.
|
| +}; |
| + |
| +/** |
| + * Returns the destination directory for media imports. Creates the |
| + * destination, if it doesn't exist. |
| + * @return {!Promise<!DirectoryEntry>} |
|
hirono
2014/12/04 04:44:25
!Promise.<!DirectoryEntry>
Ben Kwa
2014/12/04 19:53:01
Acknowledged.
|
| + */ |
| +MediaImportHandler.getImportDestination = function() { |
| + return MediaImportHandler.getDriveRoot_() |
| + .then(MediaImportHandler.getOrCreateImportDestination_); |
| +}; |
| + |
| +/** |
| + * @param {!DirectoryEntry} directory Source dir containing media for import. |
|
hirono
2014/12/04 04:44:26
Could you please add comment the task is not a tas
Ben Kwa
2014/12/04 19:53:02
Done.
|
| + * @param {!FileOperationManager} fileOperationManager |
| + * @param {!MediaScanner} mediaScanner |
| + * @constructor |
| + * @struct |
| + */ |
| +MediaImportHandler.ImportTask = |
| + function(fileOperationManager, mediaScanner, directory) { |
| + this.directory_ = directory; |
| + this.scanner_ = mediaScanner; |
| + |
| + // Call fileOperationManager.requestTaskCancel to cancel this task. |
| + // TODO(kenobi): Add task cancellation. |
| + this.taskId_ = fileOperationManager.generateTaskId(); |
| + |
| + Promise.all([ |
| + MediaImportHandler.getImportDestination(), |
|
hirono
2014/12/04 04:44:26
We use 2 spaces indent for array literals.
Ben Kwa
2014/12/04 19:53:01
Done.
|
| + this.scanner_.scan([directory]) |
| + ]).then( |
| + function(args) { |
| + /** @type {!DirectoryEntry} */ |
| + var destination = args[0]; |
| + /** @type {!Array<!FileEntry>} */ |
| + var media = args[1]; |
| + |
| + fileOperationManager.paste( |
| + media, |
| + destination, |
| + /* isMove */ false, |
| + /* opt_taskId */ this.taskId_); |
| + }.bind(this)); |
| +}; |
| + |
| +MediaImportHandler.ImportTask.prototype = { |
| + /** |
| + * @return {string} The task ID. |
| + */ |
| + get taskId() { |
| + return this.taskId_; |
| + } |
| +}; |