mirror of
https://github.com/NeteaseCloudMusicApiEnhanced/api-enhanced.git
synced 2026-10-21 15:11:22 +08:00
refactor(cloud): 重构文件处理逻辑并提取辅助函数
- 将文件大小、MD5计算等逻辑提取到 fileHelper 工具模块 - 使用统一的文件扩展名和文件名处理函数 - 简化临时文件处理和清理逻辑 - 统一文件上传数据获取方式 - 移除重复的文件操作代码并提高可维护性
This commit is contained in:
+31
-75
@@ -1,23 +1,24 @@
|
||||
const uploadPlugin = require('../plugins/songUpload')
|
||||
const crypto = require('crypto')
|
||||
const fs = require('fs')
|
||||
const createOption = require('../util/option.js')
|
||||
const logger = require('../util/logger.js')
|
||||
const {
|
||||
isTempFile,
|
||||
getFileSize,
|
||||
getFileMd5,
|
||||
cleanupTempFile,
|
||||
getFileExtension,
|
||||
sanitizeFilename,
|
||||
} = require('../util/fileHelper')
|
||||
|
||||
let mm
|
||||
module.exports = async (query, request) => {
|
||||
mm = require('music-metadata')
|
||||
let ext = 'mp3'
|
||||
if (query.songFile.name.includes('.')) {
|
||||
ext = query.songFile.name.split('.').pop()
|
||||
}
|
||||
query.songFile.name = Buffer.from(query.songFile.name, 'latin1').toString(
|
||||
'utf-8',
|
||||
)
|
||||
const filename = query.songFile.name
|
||||
.replace('.' + ext, '')
|
||||
.replace(/\s/g, '')
|
||||
.replace(/\./g, '_')
|
||||
|
||||
query.songFile.name = Buffer.from(query.songFile.name, 'latin1').toString('utf-8')
|
||||
const ext = getFileExtension(query.songFile.name)
|
||||
const filename = sanitizeFilename(query.songFile.name)
|
||||
const bitrate = 999000
|
||||
|
||||
if (!query.songFile) {
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
@@ -28,55 +29,14 @@ module.exports = async (query, request) => {
|
||||
})
|
||||
}
|
||||
|
||||
const useTempFile = !!query.songFile.tempFilePath
|
||||
let fileSize = query.songFile.size
|
||||
let fileMd5 = query.songFile.md5
|
||||
const useTemp = isTempFile(query.songFile)
|
||||
let fileSize = await getFileSize(query.songFile)
|
||||
let fileMd5 = await getFileMd5(query.songFile)
|
||||
|
||||
const cleanupTempFile = async () => {
|
||||
if (useTempFile) {
|
||||
try {
|
||||
await fs.promises.unlink(query.songFile.tempFilePath)
|
||||
} catch (e) {
|
||||
logger.info('临时文件清理失败:', e.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
query.songFile.md5 = fileMd5
|
||||
query.songFile.size = fileSize
|
||||
|
||||
try {
|
||||
if (useTempFile) {
|
||||
try {
|
||||
const stats = await fs.promises.stat(query.songFile.tempFilePath)
|
||||
fileSize = stats.size
|
||||
} catch (e) {
|
||||
logger.error('获取临时文件状态失败:', e.message)
|
||||
return Promise.reject({
|
||||
status: 500,
|
||||
body: {
|
||||
code: 500,
|
||||
msg: '临时文件访问失败',
|
||||
detail: e.message,
|
||||
},
|
||||
})
|
||||
}
|
||||
if (!fileMd5) {
|
||||
fileMd5 = await new Promise((resolve, reject) => {
|
||||
const hash = crypto.createHash('md5')
|
||||
const stream = fs.createReadStream(query.songFile.tempFilePath)
|
||||
stream.on('data', (chunk) => hash.update(chunk))
|
||||
stream.on('end', () => resolve(hash.digest('hex')))
|
||||
stream.on('error', reject)
|
||||
})
|
||||
}
|
||||
} else {
|
||||
if (!fileMd5) {
|
||||
fileMd5 = crypto.createHash('md5').update(query.songFile.data).digest('hex')
|
||||
}
|
||||
fileSize = query.songFile.data.byteLength
|
||||
}
|
||||
|
||||
query.songFile.md5 = fileMd5
|
||||
query.songFile.size = fileSize
|
||||
|
||||
const res = await request(
|
||||
`/api/cloud/upload/check`,
|
||||
{
|
||||
@@ -89,33 +49,26 @@ module.exports = async (query, request) => {
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
let artist = ''
|
||||
let album = ''
|
||||
let songName = ''
|
||||
|
||||
try {
|
||||
let metadata
|
||||
if (useTempFile) {
|
||||
if (useTemp) {
|
||||
metadata = await mm.parseFile(query.songFile.tempFilePath)
|
||||
} else {
|
||||
metadata = await mm.parseBuffer(
|
||||
query.songFile.data,
|
||||
query.songFile.mimetype,
|
||||
)
|
||||
metadata = await mm.parseBuffer(query.songFile.data, query.songFile.mimetype)
|
||||
}
|
||||
const info = metadata.common
|
||||
|
||||
if (info.title) {
|
||||
songName = info.title
|
||||
}
|
||||
if (info.album) {
|
||||
album = info.album
|
||||
}
|
||||
if (info.artist) {
|
||||
artist = info.artist
|
||||
}
|
||||
if (info.title) songName = info.title
|
||||
if (info.album) album = info.album
|
||||
if (info.artist) artist = info.artist
|
||||
} catch (error) {
|
||||
logger.info('元数据解析错误:', error.message)
|
||||
}
|
||||
|
||||
const tokenRes = await request(
|
||||
`/api/nos/token/alloc`,
|
||||
{
|
||||
@@ -189,6 +142,7 @@ module.exports = async (query, request) => {
|
||||
},
|
||||
createOption(query),
|
||||
)
|
||||
|
||||
return {
|
||||
status: 200,
|
||||
body: {
|
||||
@@ -198,6 +152,8 @@ module.exports = async (query, request) => {
|
||||
cookie: res.cookie,
|
||||
}
|
||||
} finally {
|
||||
await cleanupTempFile()
|
||||
if (useTemp) {
|
||||
await cleanupTempFile(query.songFile.tempFilePath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+6
-14
@@ -3,7 +3,10 @@ const fs = require('fs')
|
||||
var xml2js = require('xml2js')
|
||||
|
||||
const createOption = require('../util/option.js')
|
||||
const { getFileExtension, readFileChunk } = require('../util/fileHelper')
|
||||
|
||||
var parser = new xml2js.Parser()
|
||||
|
||||
function createDupkey() {
|
||||
var s = []
|
||||
var hexDigits = '0123456789abcdef'
|
||||
@@ -15,11 +18,9 @@ function createDupkey() {
|
||||
s[8] = s[13] = s[18] = s[23] = '-'
|
||||
return s.join('')
|
||||
}
|
||||
|
||||
module.exports = async (query, request) => {
|
||||
let ext = 'mp3'
|
||||
if (query.songFile.name.indexOf('flac') > -1) {
|
||||
ext = 'flac'
|
||||
}
|
||||
const ext = getFileExtension(query.songFile.name)
|
||||
const filename =
|
||||
query.songName ||
|
||||
query.songFile.name
|
||||
@@ -66,15 +67,10 @@ module.exports = async (query, request) => {
|
||||
|
||||
const useTempFile = !!query.songFile.tempFilePath
|
||||
let fileSize = query.songFile.size
|
||||
let fileData
|
||||
|
||||
if (useTempFile) {
|
||||
const stats = await fs.promises.stat(query.songFile.tempFilePath)
|
||||
fileSize = stats.size
|
||||
fileData = query.songFile.tempFilePath
|
||||
} else {
|
||||
fileSize = query.songFile.data.length
|
||||
fileData = query.songFile.data
|
||||
}
|
||||
|
||||
const blockSize = 10 * 1024 * 1024
|
||||
@@ -86,11 +82,7 @@ module.exports = async (query, request) => {
|
||||
while (offset < fileSize) {
|
||||
let chunk
|
||||
if (useTempFile) {
|
||||
const fd = await fs.promises.open(query.songFile.tempFilePath, 'r')
|
||||
const buffer = Buffer.alloc(Math.min(blockSize, fileSize - offset))
|
||||
await fd.read(buffer, 0, buffer.length, offset)
|
||||
await fd.close()
|
||||
chunk = buffer
|
||||
chunk = await readFileChunk(query.songFile.tempFilePath, offset, Math.min(blockSize, fileSize - offset))
|
||||
} else {
|
||||
chunk = query.songFile.data.slice(offset, Math.min(offset + blockSize, fileSize))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user