31 lines
1.4 KiB
TypeScript
31 lines
1.4 KiB
TypeScript
import { createHash } from 'node:crypto'
|
|
|
|
/**
|
|
* Identity of one uploaded file: every segment of it has the same poster, the
|
|
* same subject apart from the segment counter, and the same segment count.
|
|
* The group is deliberately left out so crossposts resolve to one file.
|
|
* See CONCEPT.md (D12).
|
|
*/
|
|
export function fileKey(poster: string, base: string, totalSegments: number): string {
|
|
return createHash('sha1').update(`${poster}\n${base}\n${totalSegments}`).digest('hex')
|
|
}
|
|
|
|
/** True when a yEnc name looks like a real filename (it has an extension), not a random token. */
|
|
export function looksLikeFilename(name: string): boolean {
|
|
return /\.[A-Za-z0-9]{1,5}$/.test(name) && !/^[0-9a-f]{32,}$/i.test(name)
|
|
}
|
|
|
|
/**
|
|
* Identity of a file known only from its yEnc header (obfuscated posts).
|
|
* The poster is left out: obfuscating posters randomise it per article.
|
|
* - A real filename is stable across the articles of a file, and needed to
|
|
* tell apart the equal-sized volumes of one archive.
|
|
* - A random, extensionless name can change per article too; the exact
|
|
* byte size and part count are what stays the same.
|
|
* Collisions are caught by the conflict check in IngestService.
|
|
*/
|
|
export function weakFileKey(name: string, size: number, totalParts: number): string {
|
|
const identity = looksLikeFilename(name) ? `name\n${name}` : 'shape'
|
|
return createHash('sha1').update(`weak\n${identity}\n${size}\n${totalParts}`).digest('hex')
|
|
}
|