Skip to content

Instantly share code, notes, and snippets.

@qgustavor
Last active September 30, 2015 20:19
Show Gist options
  • Save qgustavor/fb16c6b895a770ca80e3 to your computer and use it in GitHub Desktop.
Save qgustavor/fb16c6b895a770ca80e3 to your computer and use it in GitHub Desktop.
MEGA FS wrapper for Apps Script
// The returns a object similar to the returned by DriveApp.getFolderById(id):
function getFolder_(id) {
var parts = id.split('!');
if (parts.length === 2 || id.length === 8) {
var result = UrlFetchApp.fetch('https://eu.api.mega.co.nz/cs?n=' + parts[0], {
method: 'POST',
contentType: 'application/json;charset=UTF-8',
payload: '[{"a":"f","c":1,"r":1}]'
});
var jsonResult = JSON.parse(result.getContentText());
var files = jsonResult[0]['f'].filter(function (file) {
return file.k && file.t === 0;
});
var key = parts[1];
if (files.length === 0) {
throw Error('Empty folder');
}
// Recreate Google Drive API for MEGA:
return {
// Come on, that's complicated:
getFiles: function () {
var index = 0;
return {
hasNext: function () {
return index < files.length;
},
next: function () {
var fileIndex = index++
var file = files[fileIndex];
function processFileInfo() {
file.url = 'https://mega.nz/#F!' + id;
if (key) {
// getMegaFileName is defined by this file
// https://gist.github.com/qgustavor/9b9af6c8baa8693720a8
file.name = getMegaFileName(file, key);
file.url += '!' + file.name.replace(/\s/g, '_');
} else {
file.name = '[encrypted name: ' + (fileIndex + 1) + ']';
}
}
return {
getName: function () {
if (!file.name) {
processFileInfo();
}
return file.name;
},
getLastUpdated: function () {
return file.ts * 1000;
},
getUrl: function () {
if (!file.url) {
processFileInfo();
}
return file.url;
}
}
}
};
}
};
} else {
throw Error ('Invalid ID');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment