156 lines
4.2 KiB
JavaScript
156 lines
4.2 KiB
JavaScript
'use strict';
|
|
const fsp = require('fs-promise');
|
|
const path = require('path');
|
|
const Transform = require('stream').Transform;
|
|
const debug = require('debug')('psitransfer:store');
|
|
const httpErrors = require('http-errors');
|
|
|
|
class StreamLen extends Transform {
|
|
constructor(options) {
|
|
super(options);
|
|
this.bytes = 0;
|
|
}
|
|
|
|
_transform(chunk, encoding, cb) {
|
|
this.bytes += chunk.length;
|
|
this.push(chunk);
|
|
cb();
|
|
}
|
|
}
|
|
|
|
// TODO ???: make tus-store complaint: https://github.com/blockai/abstract-tus-store
|
|
class Store {
|
|
|
|
constructor(targetDir) {
|
|
this.dir = path.resolve(targetDir);
|
|
// fids with a deletion in progress
|
|
this._deleting = new Map();
|
|
}
|
|
|
|
|
|
getFilename(fid) {
|
|
if (typeof fid !== 'string' || fid.includes('\0')) {
|
|
throw new Error('file name not in jail path. aborting');
|
|
}
|
|
const base = this.dir;
|
|
const resolved = path.resolve(base, fid.replace(/\+\+/g, '/'));
|
|
const rel = path.relative(base, resolved);
|
|
if (rel === '' || rel.startsWith('..') || path.isAbsolute(rel)) {
|
|
throw new Error('file name not in jail path. aborting');
|
|
}
|
|
return resolved;
|
|
}
|
|
|
|
|
|
async create(fid, opts = {}) {
|
|
debug(`New File ${this.getFilename(fid)}`);
|
|
await fsp.ensureDir(path.dirname(this.getFilename(fid)));
|
|
await fsp.writeJson(this.getFilename(fid) + '.json', Object.assign(opts, {
|
|
isPartial: true
|
|
}));
|
|
return {uploadId: fid};
|
|
}
|
|
|
|
|
|
async update(fid, data) {
|
|
debug(`Update File ${this.getFilename(fid)}`);
|
|
await fsp.writeJson(this.getFilename(fid) + '.json', data);
|
|
return data;
|
|
}
|
|
|
|
|
|
async info(fid) {
|
|
try {
|
|
const info = await fsp.readJson(this.getFilename(fid) + '.json');
|
|
const stat = await fsp.stat(this.getFilename(fid));
|
|
info.size = stat.size;
|
|
info.offset = stat.size;
|
|
debug(`Fetched Fileinfo ${this.getFilename(fid)}`);
|
|
return info;
|
|
} catch(e) {
|
|
if(e.code === 'ENOENT') {
|
|
throw httpErrors.NotFound();
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
|
|
async append(fid, readStream, offset) {
|
|
debug(`Append Data to ${this.getFilename(fid)}`);
|
|
const uploadSize = new StreamLen();
|
|
// No `start`: a positional write on an O_APPEND fd fails with EOPNOTSUPP on
|
|
// musl >= 1.2.5 with kernels < 6.9 (no RWF_NOAPPEND); Linux ignores it anyway
|
|
const ws = fsp.createWriteStream(this.getFilename(fid), {flags: 'a'});
|
|
|
|
const ret = new Promise((resolve, reject) => {
|
|
ws.on('finish', async() => {
|
|
try {
|
|
const info = await this.info(fid);
|
|
if(info.size >= info.uploadLength) delete info.isPartial;
|
|
await fsp.writeJson(this.getFilename(fid) + '.json', info);
|
|
debug(`Finished appending Data to ${this.getFilename(fid)}`);
|
|
return resolve({ offset: info.offset, upload: info });
|
|
} catch (e) {
|
|
return reject(e);
|
|
}
|
|
});
|
|
ws.on('error', reject);
|
|
});
|
|
|
|
readStream.pipe(uploadSize).pipe(ws);
|
|
return ret;
|
|
}
|
|
|
|
|
|
createReadStream(fid, start, end, cb) {
|
|
debug(`Create ReadStream for ${this.getFilename(fid)}`);
|
|
this.info(fid).then(info => {
|
|
let contentLength = info.size;
|
|
if(start > 0) {
|
|
if(!end) end = info.size - 1;
|
|
contentLength = end - start + 1
|
|
}
|
|
if(cb) cb({ contentLength, metadata: info.metadata, info });
|
|
});
|
|
return fsp.createReadStream(this.getFilename(fid), {start, end});
|
|
}
|
|
|
|
|
|
thumbFilename(fid) {
|
|
return this.getFilename(fid) + '.thumb';
|
|
}
|
|
|
|
|
|
async writeThumb(fid, buffer) {
|
|
debug(`Write thumbnail ${this.thumbFilename(fid)}`);
|
|
await fsp.writeFile(this.thumbFilename(fid), buffer);
|
|
}
|
|
|
|
|
|
/**
|
|
* Delete a file with its metadata and thumbnail. Concurrent deletions of the
|
|
* same file share one run (Windows fails when a file is deleted twice at once).
|
|
*/
|
|
del(fid) {
|
|
if (this._deleting.has(fid)) return this._deleting.get(fid);
|
|
const run = this._del(fid).finally(() => this._deleting.delete(fid));
|
|
this._deleting.set(fid, run);
|
|
return run;
|
|
}
|
|
|
|
|
|
async _del(fid) {
|
|
debug(`Delete ${this.getFilename(fid)}`);
|
|
for (const suffix of ['.json', '.thumb', '']) {
|
|
try {
|
|
await fsp.unlink(this.getFilename(fid) + suffix);
|
|
} catch (e) {
|
|
if (e.code !== 'ENOENT') throw e;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
module.exports = Store;
|