1
0
mirror of https://github.com/Foltik/Shimapan synced 2024-11-14 08:49:51 -05:00
shimapan/app/util/upload/disk.js

28 lines
760 B
JavaScript

const fs = require('fs');
const fsPromises = fs.promises;
const mkdir = path => new Promise((resolve, reject) => {
fsPromises.mkdir(path)
.then(resolve)
.catch(err => {
if (err.code === 'EEXIST')
resolve();
else
reject(err);
});
});
const write = (path, stream) => new Promise((resolve, reject) => {
const outStream = fs.createWriteStream(path);
stream.pipe(outStream);
outStream.on('error', reject);
outStream.on('close', () => resolve(outStream.bytesWritten));
outStream.on('finish', () => resolve(outStream.bytesWritten));
});
const remove = path =>
fsPromises.unlink(path);
exports.mkdir = mkdir;
exports.write = write;
exports.remove = remove;