1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-07 14:35:26 +02:00

fix files uploading

This commit is contained in:
Nikita Melnikov 2022-04-21 21:46:15 +03:00
parent a4ff61811e
commit 7e3ace5f80
4 changed files with 21 additions and 4 deletions

View file

@ -34,7 +34,7 @@ class Transport {
* @returns {Promise<FileData>}
*/
public static async save(multerData: Dict, map: Dict): Promise<FileData> {
const { originalname: name, path, filename, size, mimetype } = multerData;
const { originalname: name, path, filename, size, mimetype, url } = multerData;
const file = new File({
name,
@ -42,12 +42,15 @@ class Transport {
path,
size,
mimetype,
url,
});
await file.save();
let response = file.data;
console.log(file)
if (map) {
response = Transport.composeResponse(file, map);
}
@ -95,6 +98,8 @@ class Transport {
let response = file.data;
console.log(response)
if (map) {
response = Transport.composeResponse(file, map);
}

View file

@ -10,6 +10,7 @@ const filesDb = database['files'];
* @property {string} filename - name of uploaded file
* @property {string} path - path to uploaded file
* @property {string} mimetype - file MIME type
* @property {string} url - file url
* @property {number} size - size of the file in
*/
export interface FileData {
@ -18,6 +19,7 @@ export interface FileData {
filename?: string;
path?: string;
mimetype?: string;
url?: string;
size?: number;
[key: string]: string | number | undefined;
}
@ -40,6 +42,7 @@ class File {
public path?: string;
public mimetype?: string;
public size?: number;
public url?: string;
/**
* @class
@ -99,13 +102,14 @@ class File {
* @param {FileData} fileData - info about file
*/
public set data(fileData: FileData) {
const { name, filename, path, mimetype, size } = fileData;
const { name, filename, path, mimetype, size, url } = 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;
this.url = url || this.url;
}
/**
@ -121,6 +125,7 @@ class File {
path: this.path,
mimetype: this.mimetype,
size: this.size,
url: this.url,
};
}

View file

@ -77,10 +77,17 @@ router.post('/transport/image', imageUploader, async (req: Request, res: Respons
return;
}
const fileData = {
...req.files.image[0],
url: '/uploads/' + req.files.image[0].filename,
};
console.log(fileData);
try {
Object.assign(
response,
await Transport.save(req.files.image[0], req.body.map ? JSON.parse(req.body.map) : undefined)
await Transport.save(fileData, req.body.map ? JSON.parse(req.body.map) : undefined)
);
response.success = 1;

View file

@ -55,7 +55,7 @@ export default class Editor {
},
additionalRequestData: {
map: JSON.stringify({
path: 'file:url',
url: 'file:url',
size: 'file:size',
mimetype: 'file:mime',
}),