2019-03-01 17:02:21 +01:00
|
|
|
/**
|
|
|
|
* @author d98762625 [d98762625@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2019
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import DishType from "./DishType.mjs";
|
|
|
|
import Utils, { isNodeEnvironment } from "../Utils.mjs";
|
2019-03-01 17:02:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Translation methods for file Dishes
|
|
|
|
*/
|
2019-07-05 10:44:01 +02:00
|
|
|
class DishFile extends DishType {
|
2019-03-01 17:02:21 +01:00
|
|
|
|
|
|
|
/**
|
2019-04-04 16:21:52 +02:00
|
|
|
* convert the given value to an ArrayBuffer
|
2019-03-01 17:02:21 +01:00
|
|
|
* @param {File} value
|
|
|
|
*/
|
2019-04-04 16:21:52 +02:00
|
|
|
static toArrayBuffer() {
|
2019-03-01 17:02:21 +01:00
|
|
|
DishFile.checkForValue(this.value);
|
2019-07-05 11:17:52 +02:00
|
|
|
if (isNodeEnvironment()) {
|
2019-03-14 17:33:09 +01:00
|
|
|
this.value = Utils.readFileSync(this.value);
|
2019-03-01 17:02:21 +01:00
|
|
|
} else {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
Utils.readFile(this.value)
|
2019-04-04 16:21:52 +02:00
|
|
|
.then(v => this.value = v.buffer)
|
2019-03-01 17:02:21 +01:00
|
|
|
.then(resolve)
|
|
|
|
.catch(reject);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-04-04 16:21:52 +02:00
|
|
|
* convert the given value from an ArrayBuffer
|
2019-03-01 17:02:21 +01:00
|
|
|
*/
|
2019-04-04 16:21:52 +02:00
|
|
|
static fromArrayBuffer() {
|
2019-03-01 17:02:21 +01:00
|
|
|
DishFile.checkForValue(this.value);
|
2019-04-04 16:21:52 +02:00
|
|
|
this.value = new File(this.value, "unknown");
|
2019-03-01 17:02:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default DishFile;
|