Skip to content

Commit

Permalink
fix(fs): fix to support auto naming for file uploads;
Browse files Browse the repository at this point in the history
  • Loading branch information
maslow committed Nov 15, 2021
1 parent 25c9f58 commit dd743d0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
5 changes: 4 additions & 1 deletion packages/storage-service/src/router/file/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { handleUploadFile } from "./upload"
import { handleGetFile } from "./get"
import { handleMakeDir } from "./mkdir"
import { handleDeleteFile } from "./delete"
import { generateUUID } from "../../lib/utils"
import path = require("path")

export const FileRouter = express.Router()

Expand All @@ -12,7 +14,8 @@ export const FileRouter = express.Router()
*/
const storage = multer.diskStorage({
filename: (_req, file, cb) => {
cb(null, file.originalname)
const { ext } = path.parse(file.originalname)
cb(null, generateUUID() + ext)
},
})
const uploader = multer({ storage })
Expand Down
13 changes: 8 additions & 5 deletions packages/storage-service/src/router/file/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
const parent = req.query?.path as string || "/"
const bucket_name = req.params.bucket as string
const token = req.query?.token as string
const auto_name = Number(req.query?.auto ?? 0)

// check given params
if (!req.file) {
Expand All @@ -30,10 +31,12 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
return res.status(400).send('bucket not found')
}

const filename = auto_name ? req.file.filename : req.file.originalname

// check file permissions
const filename = path.join(parent, req.file.originalname)
const filename_full = path.join(parent, filename)
if (bucket.mode !== BucketMode.PUBLIC_READ_WRITE) {
const [code, message] = checkFileOperationToken(bucket, token, FS_OPERATION.WRITE, filename)
const [code, message] = checkFileOperationToken(bucket, token, FS_OPERATION.WRITE, filename_full)
if (code) {
return res.status(code).send(message)
}
Expand All @@ -45,19 +48,19 @@ export async function handleUploadFile(req: express.Request, res: express.Respon
}

// check if file already exist
if (await pathExists(bucket_name, filename)) {
if (await pathExists(bucket_name, filename_full)) {
return res.send({ code: 'ALREADY_EXISTED', error: "file already exists" })
}

// construct file metadata
const metadata: FileItemMeta = {
contentType: req.file.mimetype,
parent,
name: req.file.originalname,
name: filename,
}

// start save
const storage = new GridFSStorage(bucket_name, DatabaseAgent.db)
const data = await storage.save(req.file.path, filename, metadata)
const data = await storage.save(req.file.path, filename_full, metadata)
return res.send({ code: 0, data })
}
2 changes: 1 addition & 1 deletion packages/system-client/src/views/storage/files.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
上传文件
</el-button>
<el-button size="mini" plain class="filter-item" type="default" icon="el-icon-new" @click="createDirectory">
新建目录
新建文件夹
</el-button>
<div class="filter-item" style="margin-left: 20px;">
<span style="font-size: 16px;color: gray; margin-right: 5px;">当前:</span>
Expand Down

0 comments on commit dd743d0

Please sign in to comment.