Skip to content

Commit

Permalink
🎉 use theirs origin filenames when save files
Browse files Browse the repository at this point in the history
  • Loading branch information
songquanpeng committed Dec 3, 2022
1 parent f7e970a commit 2653640
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion common/util.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const { lexer, parser } = require('marked');
const fs = require('fs');

function titleToLink(title) {
return title.trim().replace(/\s/g, '-');
Expand Down Expand Up @@ -53,9 +54,14 @@ function parseTagStr(tag) {
return [category, tags];
}

async function fileExists(path) {
return !!(await fs.promises.stat(path).catch(e => false));
}

module.exports = {
titleToLink,
parseTagStr,
getDate,
md2html
md2html,
fileExists
};
20 changes: 16 additions & 4 deletions middlewares/upload.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,27 @@
const multer = require('multer');
const uuid = require('uuid/v1');
const path = require('path');
const { fileExists, getDate } = require('../common/util');
const uploadPath = require('../config').uploadPath;

exports.upload = multer({
storage: multer.diskStorage({
destination: function(req, file, callback) {
callback(null, uploadPath);
},
filename: function(req, file, callback) {
let extension = file.originalname.split('.').pop();
file.id = uuid() + '.' + extension;
filename: async function(req, file, callback) {
if (await fileExists(path.join(uploadPath, file.originalname))) {
let parts = file.originalname.split('.');
let extension = "";
if (parts.length > 1) {
extension = parts.pop();
}
file.id = parts.join('.') + getDate("_yyyyMMddhhmmss");
if (extension) {
file.id += "." + extension;
}
} else {
file.id = file.originalname;
}
callback(null, file.id);
}
})
Expand Down

0 comments on commit 2653640

Please sign in to comment.