mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: storage: avoid special chars in filename
This commit is contained in:
1 file changed
+6
-2
@@ -13,16 +13,20 @@ import * as system from './system';
|
|||||||
export class StorageModel {
|
export class StorageModel {
|
||||||
static coll = db.collection('storage');
|
static coll = db.collection('storage');
|
||||||
|
|
||||||
|
static generateId(ext: string) {
|
||||||
|
return `${nanoid(3).replace(/[_-]/g, '0')}/${nanoid().replace(/[_-]/g, '0')}${ext}`;
|
||||||
|
}
|
||||||
|
|
||||||
static async put(path: string, file: string | Buffer | Readable, owner?: number) {
|
static async put(path: string, file: string | Buffer | Readable, owner?: number) {
|
||||||
const meta = {};
|
const meta = {};
|
||||||
await StorageModel.del([path]);
|
await StorageModel.del([path]);
|
||||||
meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out'))
|
meta['Content-Type'] = (path.endsWith('.ans') || path.endsWith('.out'))
|
||||||
? 'text/plain'
|
? 'text/plain'
|
||||||
: lookup(path) || 'application/octet-stream';
|
: lookup(path) || 'application/octet-stream';
|
||||||
let _id = `${nanoid(3)}/${nanoid()}${extname(path)}`;
|
let _id = StorageModel.generateId(extname(path));
|
||||||
// Make sure id is not used
|
// Make sure id is not used
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
while (await StorageModel.coll.findOne({ _id })) _id = `${nanoid(3)}/${nanoid()}${extname(path)}`;
|
while (await StorageModel.coll.findOne({ _id })) _id = StorageModel.generateId(extname(path));
|
||||||
await storage.put(_id, file, meta);
|
await storage.put(_id, file, meta);
|
||||||
const { metaData, size, etag } = await storage.getMeta(_id);
|
const { metaData, size, etag } = await storage.getMeta(_id);
|
||||||
await StorageModel.coll.insertOne({
|
await StorageModel.coll.insertOne({
|
||||||
|
|||||||
Reference in new issue
Block a user