2022-03-05 22:57:23 +04:00
|
|
|
import database from '../utils/database/index';
|
|
|
|
|
|
|
|
const filesDb = database['files'];
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
/**
|
2020-05-09 05:38:25 +03:00
|
|
|
* @typedef {object} FileData
|
2019-03-11 18:44:00 +03:00
|
|
|
*
|
|
|
|
* @property {string} _id - file id
|
|
|
|
* @property {string} name - original file name
|
|
|
|
* @property {string} filename - name of uploaded file
|
|
|
|
* @property {string} path - path to uploaded file
|
|
|
|
* @property {string} mimetype - file MIME type
|
|
|
|
* @property {number} size - size of the file in
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
export interface FileData {
|
|
|
|
_id?: string;
|
|
|
|
name?: string;
|
|
|
|
filename?: string;
|
|
|
|
path?: string;
|
|
|
|
mimetype?: string;
|
|
|
|
size?: number;
|
|
|
|
[key: string]: string | number | undefined;
|
|
|
|
}
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @class File
|
|
|
|
* @class File model
|
|
|
|
*
|
|
|
|
* @property {string} _id - file id
|
|
|
|
* @property {string} name - original file name
|
|
|
|
* @property {string} filename - name of uploaded file
|
|
|
|
* @property {string} path - path to uploaded file
|
|
|
|
* @property {string} mimetype - file MIME type
|
|
|
|
* @property {number} size - size of the file in
|
|
|
|
*/
|
|
|
|
class File {
|
2022-03-05 22:57:23 +04:00
|
|
|
public _id?: string;
|
|
|
|
public name?: string;
|
|
|
|
public filename?: string;
|
|
|
|
public path?: string;
|
|
|
|
public mimetype?: string;
|
|
|
|
public size?: number;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @class
|
|
|
|
*
|
|
|
|
* @param {FileData} data - info about file
|
|
|
|
*/
|
|
|
|
constructor(data: FileData = {}) {
|
|
|
|
if (data === null) {
|
|
|
|
data = {};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (data._id) {
|
|
|
|
this._id = data._id;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.data = data;
|
|
|
|
}
|
2019-03-11 18:44:00 +03:00
|
|
|
/**
|
|
|
|
* Find and return model of file with given id
|
2020-05-09 05:38:25 +03:00
|
|
|
*
|
2019-03-11 18:44:00 +03:00
|
|
|
* @param {string} _id - file id
|
|
|
|
* @returns {Promise<File>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static async get(_id: string): Promise<File> {
|
|
|
|
const data: FileData = await filesDb.findOne({ _id });
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
return new File(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find and return model of file with given id
|
2020-05-09 05:38:25 +03:00
|
|
|
*
|
2019-03-11 18:44:00 +03:00
|
|
|
* @param {string} filename - uploaded filename
|
|
|
|
* @returns {Promise<File>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static async getByFilename(filename: string): Promise<File> {
|
2019-03-11 18:44:00 +03:00
|
|
|
const data = await filesDb.findOne({ filename });
|
|
|
|
|
|
|
|
return new File(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Find all files which match passed query object
|
|
|
|
*
|
2022-03-05 22:57:23 +04:00
|
|
|
* @param {object} query - input query
|
2019-03-11 18:44:00 +03:00
|
|
|
* @returns {Promise<File[]>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public static async getAll(query: Record<string, unknown> = {}): Promise<File[]> {
|
2019-03-11 18:44:00 +03:00
|
|
|
const docs = await filesDb.find(query);
|
|
|
|
|
|
|
|
return Promise.all(docs.map(doc => new File(doc)));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set FileData object fields to internal model fields
|
|
|
|
*
|
2022-03-05 22:57:23 +04:00
|
|
|
* @param {FileData} fileData - info about file
|
2019-03-11 18:44:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public set data(fileData: FileData) {
|
2019-03-11 18:44:00 +03:00
|
|
|
const { name, filename, path, mimetype, size } = fileData;
|
|
|
|
|
|
|
|
this.name = name || this.name;
|
|
|
|
this.filename = filename || this.filename;
|
|
|
|
this.path = path ? this.processPath(path) : this.path;
|
|
|
|
this.mimetype = mimetype || this.mimetype;
|
|
|
|
this.size = size || this.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return FileData object
|
|
|
|
*
|
|
|
|
* @returns {FileData}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public get data(): FileData {
|
2019-03-11 18:44:00 +03:00
|
|
|
return {
|
|
|
|
_id: this._id,
|
|
|
|
name: this.name,
|
|
|
|
filename: this.filename,
|
|
|
|
path: this.path,
|
|
|
|
mimetype: this.mimetype,
|
2020-05-09 05:38:25 +03:00
|
|
|
size: this.size,
|
2019-03-11 18:44:00 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save or update file data in the database
|
|
|
|
*
|
|
|
|
* @returns {Promise<File>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public async save(): Promise<File> {
|
2019-03-11 18:44:00 +03:00
|
|
|
if (!this._id) {
|
2022-03-05 22:57:23 +04:00
|
|
|
const insertedRow = await filesDb.insert(this.data) as { _id: string };
|
2019-03-11 18:44:00 +03:00
|
|
|
|
|
|
|
this._id = insertedRow._id;
|
|
|
|
} else {
|
|
|
|
await filesDb.update({ _id: this._id }, this.data);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove file data from the database
|
|
|
|
*
|
|
|
|
* @returns {Promise<File>}
|
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public async destroy(): Promise<File> {
|
2019-03-11 18:44:00 +03:00
|
|
|
await filesDb.remove({ _id: this._id });
|
|
|
|
|
|
|
|
delete this._id;
|
|
|
|
|
|
|
|
return this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-05 22:57:23 +04:00
|
|
|
* Return readable file data
|
2020-05-09 05:38:25 +03:00
|
|
|
*
|
2022-03-05 22:57:23 +04:00
|
|
|
* @returns {FileData}
|
2019-03-11 18:44:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
public toJSON(): FileData {
|
|
|
|
return this.data;
|
2019-03-11 18:44:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-03-05 22:57:23 +04:00
|
|
|
* Removes unnecessary public folder prefix
|
2019-03-11 18:44:00 +03:00
|
|
|
*
|
2022-03-05 22:57:23 +04:00
|
|
|
* @param {string} path - input path to be processed
|
|
|
|
* @returns {string}
|
2019-03-11 18:44:00 +03:00
|
|
|
*/
|
2022-03-05 22:57:23 +04:00
|
|
|
private processPath(path: string): string {
|
|
|
|
return path.replace(/^public/, '');
|
2019-03-11 18:44:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-05 22:57:23 +04:00
|
|
|
export default File;
|