全新出发
This commit is contained in:
1 parent
b06c117b1e
commit
fa4edebed6
463 files changed
+181610
-125
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import * as crypto from 'crypto'
|
||||
|
||||
export async function getMediaFileStat(sourcePath: string): Promise<{ size: number; mtimeMs: number } | null> {
|
||||
try {
|
||||
const stat = await fs.promises.stat(sourcePath)
|
||||
if (!stat.isFile()) return null
|
||||
return {
|
||||
size: Number.isFinite(stat.size) ? Math.max(0, Math.floor(stat.size)) : 0,
|
||||
mtimeMs: Number.isFinite(stat.mtimeMs) ? Math.max(0, Math.floor(stat.mtimeMs)) : 0
|
||||
}
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
export function buildMediaFileCachePath(
|
||||
cacheRoot: string,
|
||||
kind: 'image' | 'video' | 'emoji',
|
||||
sourcePath: string,
|
||||
fileStat: { size: number; mtimeMs: number }
|
||||
): string {
|
||||
const normalizedSource = path.resolve(sourcePath)
|
||||
const rawKey = `${kind}\u001f${normalizedSource}\u001f${fileStat.size}\u001f${fileStat.mtimeMs}`
|
||||
const digest = crypto.createHash('sha1').update(rawKey).digest('hex')
|
||||
const ext = path.extname(normalizedSource) || ''
|
||||
return path.join(cacheRoot, kind, digest.slice(0, 2), `${digest}${ext}`)
|
||||
}
|
||||
Reference in new issue
Block a user