Skip to content

Instantly share code, notes, and snippets.

@akirattii
Created July 15, 2016 10:31
Show Gist options
  • Save akirattii/7a2880f5c86df89fead2fe579be1ef97 to your computer and use it in GitHub Desktop.
Save akirattii/7a2880f5c86df89fead2fe579be1ef97 to your computer and use it in GitHub Desktop.
filesystem manager includes opening file dialog, csv manipulation etc.
/**
* File Manager for Chrome apps
*/
var fileManager = function() {
/**
* write a file using opendir dialog interactively.
* @param {String} filename
* @param {String} text to output into the file
* @param {Function} callback(function(err, fileEntry){...}) Optional
*/
function writeInteractively(filename, text, cb) {
getFileEntryByOpenDir(filename, function(err, fileEntry) {
if (chrome.runtime.lastError) {
return cb && cb(chrome.runtime.lastError);
}
fileEntry.createWriter(function(fileWriter) {
var truncated = false;
var data = null;
fileWriter.onwriteend = function(e) {
fileWriter.onwriteend = null; // Avoid an infinite loop.
// 余分なデータ尻をちょん切る。
e.currentTarget.truncate(e.currentTarget.position);
return cb && cb(null, fileEntry);
};
fileWriter.onerror = function(e) {
return cb && cb(e);
};
data = new Blob([text], { type: 'text/plain' });
fileWriter.write(data);
}, function(e) {
return cb && cb(e);
});
});
}
/**
* read file using openfile dialog interactively.
* @param {Function} callback(function(err, fileEntry){...})
*/
function readInteractively(charset, cb) {
getFileEntryByOpenFile(function(err, fileEntry) {
if (chrome.runtime.lastError) {
return cb && cb(chrome.runtime.lastError);
}
// Get a File object representing the file,
// then use FileReader to read its contents.
fileEntry.file(function(file) {
let reader = new FileReader();
reader.onloadend = function(e) {
return cb && cb(null, this);
};
if (charset)
reader.readAsText(file, charset);
else
reader.readAsText(file);
}, function(e) {
return cb && cb(e);
});
});
}
function getFileEntryByOpenDir(filename, cb) {
let opts = { create: true }; // add "exclusive: true" to prevent overwrite
chrome.fileSystem.chooseEntry({ 'type': "openDirectory" }, function(dirEntry) {
if (!dirEntry) {
return cb && cb("chooseEntry cancelled.");
}
dirEntry.getFile(
filename, opts,
function(fileEntry) {
return cb && cb(null, fileEntry);
},
function(e) {
return cb && cb(e);
}
);
});
}
function getFileEntryByOpenFile(cb) {
chrome.fileSystem.chooseEntry({ 'type': "openFile" }, function(fileEntry) {
if (!fileEntry) {
return cb && cb("chooseEntry cancelled.");
}
return cb && cb(null, fileEntry);
});
}
function getDomInteractively(cb) {
readInteractively(function(err, reader) {
if (err) return cb && cb(err);
let el = document.createElement('html');
el.innerHTML = reader.result;
return cb && cb(null, el);
});
}
function parseCsvInteractively(charset, cb) {
readInteractively(charset, function(err, fr) {
console.log("err", err, "fr", fr);
return cb(err, parseCsv(fr.result));
});
}
function parseCsv(csvStr) {
let rows = csvStr.split("\n");
let len = rows.length;
let row;
let cols;
let ret = [];
for (let i = 0; i < len; i++) {
row = rows[i];
cols = row.split(",");
ret.push(cols);
}
return ret;
}
//
// -- public methods
return {
writeInteractively,
readInteractively,
getDomInteractively,
parseCsvInteractively,
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment