Created
December 28, 2013 08:34
-
-
Save anonymous/8157384 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var gGalleryIndex = 0; // gallery currently being iterated | |
var gGalleryReader = null; // the filesytem reader for the current gallery | |
var gDirectories = []; // used to process subdirectories | |
var gGalleryArray = []; // holds information about all top-level Galleries found - list of DomFileSystem | |
var gGalleryData = []; // hold computed information about each Gallery | |
var gCurOptGrp = null; | |
var vidFormats = ["webm", "mp4"]; | |
function errorPrintFactory(custom) { | |
return function(e) { | |
var msg = ""; | |
switch (e.code) { | |
case FileError.QUOTA_EXCEEDED_ERR: | |
msg = "QUOTA_EXCEEDED_ERR"; | |
break; | |
case FileError.NOT_FOUND_ERR: | |
msg = "NOT_FOUND_ERR"; | |
break; | |
case FileError.SECURITY_ERR: | |
msg = "SECURITY_ERR"; | |
break; | |
case FileError.INVALID_MODIFICATION_ERR: | |
msg = "INVALID_MODIFICATION_ERR"; | |
break; | |
case FileError.INVALID_STATE_ERR: | |
msg = "INVALID_STATE_ERR"; | |
break; | |
default: | |
msg = "Unknown Error"; | |
break; | |
}; | |
console.log(custom + ": " + msg); | |
}; | |
} | |
function GalleryData(id) { | |
this._id = id; | |
this.path = ""; | |
this.sizeBytes = 0; | |
this.numFiles = 0; | |
this.numDirs = 0; | |
} | |
function addVideoToContentDiv() { | |
var content_div = document.getElementById("content"); | |
var video = document.createElement("video"); | |
content_div.appendChild(video); | |
return video; | |
} | |
function getFileType(filename) { | |
var ext = filename.substr(filename.lastIndexOf(".") + 1).toLowerCase(); | |
if (vidFormats.indexOf(ext) >= 0) | |
return "video"; | |
else return null; | |
} | |
function clearContentDiv() { | |
var content_div = document.getElementById("content"); | |
while (content_div.childNodes.length >= 1) { | |
content_div.removeChild(content_div.firstChild); | |
} | |
} | |
function clearList() { | |
document.getElementById("gallery-list").innerHTML = ""; | |
} | |
function updateSelection(e) { | |
var selList = document.getElementById("gallery-list"); | |
var indx = selList.selectedIndex; | |
var fsId = selList.options[indx].getAttribute("data-fsid"); | |
var fs = null; | |
// Get the filesystem that the selected file belongs to | |
for (var i=0; i < gGalleryArray.length; i++) { | |
var mData = chrome.mediaGalleries.getMediaFileSystemMetadata(gGalleryArray[i]); | |
if (mData.galleryId == fsId) { | |
fs = gGalleryArray[i]; | |
break; | |
} | |
} | |
if (fs) { | |
var path = selList.options[indx].getAttribute("data-fullpath"); | |
fs.root.getFile(path, {create: false}, function(fileEntry) { | |
var url = fileEntry.toURL(); | |
var newElem = null; | |
// Show the file data | |
clearContentDiv(); | |
var type = getFileType(path); | |
if (type == "video") | |
newElem = addVideoToContentDiv(); | |
if (newElem) { | |
(function(image_element) { | |
fileEntry.file(function(fff) { | |
var reader = new FileReader(); | |
reader.onerror = errorPrintFactory("FileReader"); | |
reader.onloadend = function(e) { | |
image_element.src = this.result; | |
}; | |
reader.readAsDataURL(fff); | |
}, errorPrintFactory("PlayBack")); | |
}(newElem)); | |
} | |
}); | |
} | |
} | |
function addGallery(name, id) { | |
var optGrp = document.createElement("optgroup"); | |
optGrp.setAttribute("label",name); | |
optGrp.setAttribute("id", id); | |
document.getElementById("gallery-list").appendChild(optGrp); | |
return optGrp; | |
} | |
function addItem(itemEntry) { | |
var opt = document.createElement("option"); | |
if (itemEntry.isFile) { | |
opt.setAttribute("data-fullpath", itemEntry.fullPath); | |
var mData = chrome.mediaGalleries.getMediaFileSystemMetadata(itemEntry.filesystem); | |
opt.setAttribute("data-fsid", mData.galleryId); | |
} | |
opt.appendChild(document.createTextNode(itemEntry.name)); | |
gCurOptGrp.appendChild(opt); | |
} | |
function scanGallery(entries) { | |
// When the size of the entries array is 0, we've processed all the directory contents | |
if (entries.length == 0) { | |
if (gDirectories.length > 0) { | |
var dir_entry = gDirectories.shift(); | |
console.log("Doing subdir: " + dir_entry.fullPath); | |
gGalleryReader = dir_entry.createReader(); | |
gGalleryReader.readEntries(scanGallery, errorPrintFactory("readEntries")); | |
} | |
else { | |
gGalleryIndex++; | |
if (gGalleryIndex < gGalleryArray.length) { | |
console.log("Doing next Gallery: " + gGalleryArray[gGalleryIndex].name); | |
scanGalleries(gGalleryArray[gGalleryIndex]); | |
} | |
} | |
return; | |
} | |
for (var i = 0; i < entries.length; i++) { | |
console.log(entries[i].name); | |
if (entries[i].isFile) { | |
addItem(entries[i]); | |
gGalleryData[gGalleryIndex].numFiles++; | |
(function(galData) { | |
entries[i].getMetadata(function(metadata){ | |
galData.sizeBytes += metadata.size; | |
}); | |
}(gGalleryData[gGalleryIndex])); | |
} | |
else if (entries[i].isDirectory) { | |
gDirectories.push(entries[i]); | |
} | |
else { | |
console.log("Got something other than a file or directory."); | |
} | |
} | |
// readEntries has to be called until it returns an empty array. According to the spec, | |
// the function might not return all of the directory's contents during a given call. | |
gGalleryReader.readEntries(scanGallery, errorPrintFactory("readMoreEntries")); | |
} | |
function scanGalleries(fs) { | |
var mData = chrome.mediaGalleries.getMediaFileSystemMetadata(fs); | |
console.log("Reading gallery: " + mData.name); | |
gCurOptGrp = addGallery(mData.name, mData.galleryId); | |
gGalleryData[gGalleryIndex] = new GalleryData(mData.galleryId); | |
gGalleryReader = fs.root.createReader(); | |
gGalleryReader.readEntries(scanGallery, errorPrintFactory("readEntries")); | |
} | |
function getGalleriesInfo(results) { | |
clearContentDiv(); | |
if (results.length) { | |
var str = "Gallery count: " + results.length + " ( "; | |
results.forEach(function(item, indx, arr) { | |
var mData = chrome.mediaGalleries.getMediaFileSystemMetadata(item); | |
if (mData) { | |
str += mData.name; | |
if (indx < arr.length-1) | |
str += ","; | |
str += " "; | |
} | |
}); | |
str += ")"; | |
document.getElementById("filename").innerText = str; | |
gGalleryArray = results; // store the list of gallery directories | |
gGalleryIndex = 0; | |
document.getElementById("scan-button").disabled = ""; | |
} | |
else { | |
document.getElementById("filename").innerText = "No galleries found"; | |
document.getElementById("scan-button").disabled = "disabled"; | |
} | |
} | |
window.addEventListener("load", function() { | |
// __MGA__bRestart is set in the launcher code to indicate that the app was | |
// restarted instead of being normally launched | |
if (window.__MGA__bRestart) { | |
console.log("App was restarted"); | |
// if the app was restarted, get the media gallery information | |
chrome.mediaGalleries.getMediaFileSystems({ | |
interactive : "if_needed" | |
}, getGalleriesInfo); | |
} | |
document.getElementById("gallery-button").addEventListener("click", function() { | |
chrome.mediaGalleries.getMediaFileSystems({ | |
interactive : "if_needed" | |
}, getGalleriesInfo); | |
}); | |
document.getElementById("configure-button").addEventListener("click", function() { | |
chrome.mediaGalleries.getMediaFileSystems({ | |
interactive : "yes" | |
}, getGalleriesInfo); | |
}); | |
document.getElementById("scan-button").addEventListener("click", function () { | |
clearContentDiv(); | |
clearList(); | |
if (gGalleryArray.length > 0) { | |
scanGalleries(gGalleryArray[0]); | |
} | |
}); | |
document.getElementById("gallery-list").addEventListener("change", function(e) { | |
updateSelection(e); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment