Add filetype/read/readBuffer functions to FileSetting

This commit is contained in:
Maks-s 2018-08-21 16:01:37 +02:00
parent 3c38ecdd97
commit 9711c99217
No known key found for this signature in database
GPG Key ID: 13E69BA53A9E913B
1 changed files with 64 additions and 0 deletions

View File

@ -58,4 +58,68 @@ export default class FileSetting extends Setting {
return files.length ? files.join(', ') : '()';
}
/**
* Returns an array with the mime type of files in this setting's value
* @return {Array|Promise}
*/
async filetype() {
if (!this.value || !this.value.length) return [];
const files = [];
for (const filepath of this.value) {
const resolvedPath = path.resolve(this.path, filepath);
const type = await FileUtils.getFileType(resolvedPath);
if (type === null)
throw {message: `Unknown mime type for file ${resolvedPath}`};
files.push([
filepath,
type.ext,
type.mime
]);
}
return files;
}
/**
* Returns an array with the contents of files in this file setting's value
* @return {Promise}
*/
async read() {
if (!this.value || !this.value.length) return [];
const files = [];
for (const filepath of this.value) {
const data = await FileUtils.readFile(path.resolve(this.path, filepath));
files.push([
filepath,
data
]);
}
return files;
}
/**
* Returns an array with the contents of files in this file setting's value
* @param {Object} options Additional options to pass to FileUtils.readFileBuffer
* @return {Promise}
*/
async readBuffer(options) {
if (!this.value || !this.value.length) return [];
const files = [];
for (const filepath of this.value) {
const data = await FileUtils.readFileBuffer(path.resolve(this.path, filepath), options);
files.push([
filepath,
data
]);
}
return files;
}
}