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

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

Issue 739263003: Add a media scanner convenience class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 1 month 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/media_scanner.js
diff --git a/ui/file_manager/file_manager/background/js/media_scanner.js b/ui/file_manager/file_manager/background/js/media_scanner.js
new file mode 100644
index 0000000000000000000000000000000000000000..7c744b2e214768e5bcc90d94c3b31f1c1f8f2c6b
--- /dev/null
+++ b/ui/file_manager/file_manager/background/js/media_scanner.js
@@ -0,0 +1,65 @@
+// 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.
+
+/**
+ * Recursively scans through a list of given files and directories, and creates
+ * a list of media files.
+ *
+ * @param {!Array.<!Entry>} entries A list of file and directory entries. File
+ * entries are added directly to the media list; directory entries are
+ * recursively traversed to find files, which are added to the media list.
+ * @constructor
+ */
+function MediaScanner(entries) {
+ /** @private {!Promise<!Array<!FileEntry>>} */
+ this.filesPromise_ = this.scan_(entries);
+}
+
+/**
+ * Scans a list of directory and file entries, returning image and video files.
+ * @param {!Array<!Entry>} entries
+ */
+MediaScanner.prototype.scan_ = function(entries) {
+ /**
+ * Returns files and directories found under the given Entry.
+ * @param {!Entry} entry
+ * @return {!Promise<!Array<!Entry>>}
+ */
+ var scanRecurse = function(entry) {
+ return new Promise(function(resolve, reject) {
+ fileOperationUtil.resolveRecursively(entry, resolve, reject);
+ });
+ };
+
+ /**
+ * Flattens a nested list of Entries.
+ * @param {!Array<!Array<!Entry>>} array
+ * @return {!Array<!Entry>}
+ */
+ var flatten = function(array) {
+ return array.reduce(function(prev, curr) {
+ return prev.concat(curr);
+ }, []);
+ };
+
+ /**
+ * Filters non-image and non-video files out of the given list.
+ * @param {!Array<!Entry>} array
+ * @return {!Array<!Entry>}
+ */
+ var filter = function(array) {
+ return array.filter(FileType.isImageOrVideo);
+ };
+
+ return Promise.all(entries.map(scanRecurse))
+ .then(flatten)
+ .then(filter);
+};
+
+/**
+ * @return {!Promise<!Array<!FileEntry>>}
+ */
+MediaScanner.prototype.getFiles = function() {
+ return this.filesPromise_;
+};
« no previous file with comments | « ui/file_manager/file_manager/background/js/compiled_resources.gyp ('k') | ui/file_manager/file_manager/manifest.json » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698