Last active
March 9, 2017 11:58
-
-
Save dwolner/14f61e7683142675efae to your computer and use it in GitHub Desktop.
function in cordova to encrypt media with custom plugin
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
function initFS() { | |
//init and maintain fs, look into removing | |
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fs) { | |
appFileSystem = fs; | |
fs.root.getDirectory("Wanagogo", {create: true, exclusive: false}, function(dirEntry) { | |
mainAppDir = dirEntry | |
k('defining main app dir', dirEntry) | |
//look at downloads | |
mainAppDir.getDirectory("downloads", {create: true, exclusive: false}, function(dirEntry) { | |
wggApi.logEvent({ | |
eventType: "loadedFilesystem", | |
elapsed: ((Date.now() - window.deviceReadyTime) /1000).toFixed(2) | |
// elapsed: Math.round((Date.now() - window.deviceReadyTime) /1000) | |
}) | |
downloadDir = dirEntry; | |
//audit downloads | |
var directoryReader = dirEntry.createReader() | |
directoryReader.readEntries( function(entries) { | |
auditDownloads(entries) | |
}, fsError) | |
}, fsError) | |
if (device.platform == 'Android') { | |
// window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, function(fileSystem) { | |
mainAppDir.getFile('wggLog.txt', {create:true}, function(file) { | |
WGGLOG = file | |
k("got the file", JSON.stringify(file)); | |
}, fsError) | |
} | |
}, fsError) | |
}, fsError) | |
} | |
function encryptMedia(SKU) { | |
k('process media file: ' + SKU) | |
var self = this | |
var theBegin = Date.now() | |
var fileName = SKU + '.mp4' | |
var random = randString(32) | |
var encryptObj = { | |
fileName: fileName, | |
key: random, | |
mutated: false | |
} | |
wggApi.localStore(SKU, JSON.stringify(encryptObj)) | |
k('mutate fileName: ', fileName) // wngg_MCPP00VL0004D.mp4 | |
//look at downloads | |
mainAppDir.getDirectory("downloads", {create: true, exclusive: false}, function(dirEntry) { | |
dirEntry.getFile(fileName, {"create": true, "exclusive": false}, function(fileEntry) { | |
var filepath = fileEntry.toURL(); | |
k('mutate filepath: ', filepath) // ms-appdata:///local//Wanagogo/downloads/wngg_MCPP00VL0004D.mp4 | |
MutateFile.mutate(filepath, encryptObj.key, function (message) { | |
k("mutate success: " + message) | |
encryptObj.mutated = true; | |
encryptObj.filePath = filepath; | |
wggApi.localStore(SKU, JSON.stringify(encryptObj)) | |
}, function (e) { | |
k("mutate fail: ", e) | |
}) | |
}, fsError) | |
}, fsError) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment