Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(547)

Unified Diff: ui/file_manager/file_manager/background/js/duplicate_finder.js

Issue 885653003: Files.app: Add deduplication code for imports to Google Drive. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Address feedback. Created 5 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ui/file_manager/file_manager/background/js/duplicate_finder.js
diff --git a/ui/file_manager/file_manager/background/js/duplicate_finder.js b/ui/file_manager/file_manager/background/js/duplicate_finder.js
index 70439cdd9e52efce49c60f17cf7af214a061473b..25ac1574af6574271a5e93291990483546752b0f 100644
--- a/ui/file_manager/file_manager/background/js/duplicate_finder.js
+++ b/ui/file_manager/file_manager/background/js/duplicate_finder.js
@@ -26,26 +26,97 @@ importer.DuplicateFinder.prototype.checkDuplicate;
* @implements {importer.DuplicateFinder}
* @struct
*/
-importer.DriveDuplicateFinder = function() {};
+importer.DriveDuplicateFinder = function() {
+ /** @private {Promise<string>} */
+ this.driveIdPromise_ = null;
+};
/** @override */
importer.DriveDuplicateFinder.prototype.checkDuplicate = function(entry) {
- // TODO(kenobi): Implement content hash deduplication.
- return Promise.resolve(false);
+ return importer.DriveDuplicateFinder.computeHash_(entry)
+ .then(this.findByHash_.bind(this))
+ .then(
+ /**
+ * @param {!Array<string>} urls
+ * @return {boolean}
+ */
+ function(urls) {
+ return urls.length > 0;
+ });
};
/**
- * A duplicate finder for testing. Allows the return value to be set.
- * @constructor
- * @implements {importer.DuplicateFinder}
- * @struct
+ * Computes the content hash for the given file entry.
+ * @param {!FileEntry} entry
+ * @private
*/
-importer.TestDuplicateFinder = function() {
- /** @type {boolean} */
- this.returnValue = false;
+importer.DriveDuplicateFinder.computeHash_ = function(entry) {
+ return new Promise(
+ function(resolve, reject) {
+ chrome.fileManagerPrivate.computeChecksum(
+ entry.toURL(),
+ /** @param {string} result The content hash. */
+ function(result) {
+ if (chrome.runtime.lastError) {
+ reject(chrome.runtime.lastError);
+ } else {
+ resolve(result);
+ }
+ });
+ });
};
-/** @override */
-importer.TestDuplicateFinder.prototype.checkDuplicate = function(entry) {
- return Promise.resolve(this.returnValue);
+/**
+ * Finds files with content hashes matching the given hash.
+ * @param {string} hash The content hash of the file to find.
+ * @return {!Promise<Array<string>>} The URLs of the found files. If there are
+ * no matches, the list will be empty.
+ * @private
+ */
+importer.DriveDuplicateFinder.prototype.findByHash_ = function(hash) {
+ return this.getDriveId_()
+ .then(importer.DriveDuplicateFinder.searchFilesByHash_.bind(null, hash));
+};
+
+/**
+ * @return {!Promise<string>} ID of the user's Drive volume.
+ * @private
+ */
+importer.DriveDuplicateFinder.prototype.getDriveId_ = function() {
+ if (!this.driveIdPromise_) {
+ this.driveIdPromise_ = VolumeManager.getInstance()
+ .then(
+ /**
+ * @param {!VolumeManager} volumeManager
+ * @return {string} ID of the user's Drive volume.
+ */
+ function(volumeManager) {
+ return volumeManager.getCurrentProfileVolumeInfo(
+ VolumeManagerCommon.VolumeType.DRIVE).volumeId;
+ });
+ }
+ return this.driveIdPromise_;
+};
+
+/**
+ * A promise-based wrapper for chrome.fileManagerPrivate.searchFilesByHashes.
+ * @param {string} hash The content hash to search for.
+ * @param {string} volumeId The volume to search.
+ * @return <!Promise<Array<string>>> A list of file URLs.
+ */
+importer.DriveDuplicateFinder.searchFilesByHash_ = function(hash, volumeId) {
+ return new Promise(
+ function(resolve, reject) {
+ chrome.fileManagerPrivate.searchFilesByHashes(
+ volumeId,
+ [hash],
+ /** @param {!Object<string, Array<string>>} urls */
+ function(urls) {
+ if (chrome.runtime.lastError) {
+ reject(chrome.runtime.lastError);
+ } else {
+ resolve(urls[hash]);
+ }
+ });
+ });
};

Powered by Google App Engine
This is Rietveld 408576698