-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b25627e
commit 11391f1
Showing
5 changed files
with
4,691 additions
and
3,702 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./music"; | ||
export * from "./upload"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import express, { Router } from "express"; | ||
import { auth } from "middleware/authentication"; | ||
import { getAppContext } from "@/ctx"; | ||
import sharp from "sharp"; | ||
import multer from "multer"; | ||
import { ENABLE_OSS } from "@/const"; | ||
import OSS from "ali-oss"; | ||
import { log } from "@/utils"; | ||
import { parseFile, parseBuffer } from "music-metadata"; | ||
|
||
const upload = multer({ | ||
storage: ENABLE_OSS | ||
? multer.memoryStorage() | ||
: multer.diskStorage({ | ||
destination: "uploads/", | ||
}), | ||
}); | ||
|
||
const webpUpload = multer({ | ||
storage: ENABLE_OSS | ||
? multer.memoryStorage() | ||
: multer.diskStorage({ | ||
destination: "uploads/", | ||
filename: function (req, file, cb) { | ||
const uniqueSuffix = | ||
Date.now() + "-" + Math.round(Math.random() * 1e9); | ||
cb(null, `local-${uniqueSuffix}.webp`); | ||
}, | ||
}), | ||
}); | ||
|
||
const sharpImageToWebp = async (buf: Buffer) => { | ||
const output = await sharp(buf) | ||
.resize(200) | ||
.webp({ lossless: true }) | ||
.toBuffer(); | ||
return output; | ||
}; | ||
|
||
// return the Map<nanoid(from front_end),new_file_name(from multer)> | ||
// the new_file_name will store into db. And frontend will concat the value with Static path later. | ||
const ossPut = async ( | ||
client: OSS, | ||
files: Express.Multer.File[], | ||
filenameFormatter?: (name: string) => string | ||
): Promise<Record<string, string>> => { | ||
try { | ||
const entry = await Promise.all( | ||
files.map(async (file) => { | ||
const id = file.originalname; | ||
const name = ENABLE_OSS | ||
? filenameFormatter | ||
? filenameFormatter(id) | ||
: id | ||
: file.filename; | ||
|
||
if (ENABLE_OSS) { | ||
await client.put(name, file.buffer); | ||
} | ||
return [id, name]; | ||
}) | ||
); | ||
return Object.fromEntries(entry); | ||
} catch (error) { | ||
throw error; | ||
} | ||
}; | ||
|
||
export async function uploadRoute(): Promise<Router> { | ||
const { ossClient } = await getAppContext(); | ||
|
||
const route = express.Router(); | ||
|
||
route.post( | ||
"/upload/image", | ||
auth({ allowGuest: true }), | ||
webpUpload.array("file", 20), | ||
async function (req, rep) { | ||
if (!Array.isArray(req.files)) { | ||
return rep.status(400).send("files is not array."); | ||
} | ||
try { | ||
// sharp image | ||
const files: Express.Multer.File[] = await Promise.all( | ||
req.files.map(async (file) => { | ||
return { | ||
...file, | ||
buffer: file.buffer && (await sharpImageToWebp(file.buffer)), | ||
} as Express.Multer.File; | ||
}) | ||
); | ||
const fileMap = await ossPut( | ||
ossClient, | ||
files, | ||
(name) => `${name}.webp` | ||
); | ||
return rep.send(fileMap); | ||
} catch (error) { | ||
log(error); | ||
return rep.status(500).send(error); | ||
} | ||
} | ||
); | ||
|
||
route.post( | ||
"/upload/song", | ||
auth({ allowGuest: true }), | ||
upload.array("file", 20), | ||
async function (req, rep) { | ||
// TODO: check music length | ||
if (!Array.isArray(req.files)) { | ||
return rep.status(400).send("files is not array."); | ||
} | ||
try { | ||
for (const file of req.files) { | ||
const meta = file.buffer | ||
? await parseBuffer(file.buffer) | ||
: await parseFile(`uploads/${file.filename}`); | ||
if (meta.format.duration > 18) { | ||
return rep.status(400).send("Music too long."); | ||
} | ||
} | ||
} catch (error) { | ||
log(error); | ||
return rep.status(400).send((error as Error).message); | ||
} | ||
try { | ||
const fileMap = await ossPut(ossClient, req.files); | ||
return rep.send(fileMap); | ||
} catch (error) { | ||
log(error); | ||
return rep.status(400).send(`Put Music Failed`); | ||
} | ||
} | ||
); | ||
|
||
return route; | ||
} |
Oops, something went wrong.