From 5772f679fb476b9e85a6e8a6379ad468cc78acb7 Mon Sep 17 00:00:00 2001 From: the1812 Date: Fri, 30 Jun 2023 23:26:43 +0800 Subject: [PATCH] Delete unused files --- src/flac-tagger/buffer-base.ts | 10 -- src/flac-tagger/index.ts | 93 ---------- src/flac-tagger/metadata-block/header.ts | 46 ----- src/flac-tagger/metadata-block/index.ts | 9 - src/flac-tagger/metadata-block/other.ts | 28 --- src/flac-tagger/metadata-block/parse.ts | 16 -- src/flac-tagger/metadata-block/picture.ts | 161 ------------------ .../metadata-block/vorbis-comment.ts | 100 ----------- src/flac-tagger/stream.ts | 67 -------- 9 files changed, 530 deletions(-) delete mode 100644 src/flac-tagger/buffer-base.ts delete mode 100644 src/flac-tagger/index.ts delete mode 100644 src/flac-tagger/metadata-block/header.ts delete mode 100644 src/flac-tagger/metadata-block/index.ts delete mode 100644 src/flac-tagger/metadata-block/other.ts delete mode 100644 src/flac-tagger/metadata-block/parse.ts delete mode 100644 src/flac-tagger/metadata-block/picture.ts delete mode 100644 src/flac-tagger/metadata-block/vorbis-comment.ts delete mode 100644 src/flac-tagger/stream.ts diff --git a/src/flac-tagger/buffer-base.ts b/src/flac-tagger/buffer-base.ts deleted file mode 100644 index 4be894b..0000000 --- a/src/flac-tagger/buffer-base.ts +++ /dev/null @@ -1,10 +0,0 @@ -export abstract class BufferBase { - abstract toBuffer(): Buffer - abstract get length(): number -} - -export const allocBufferAndWrite = (size: number, onWrite: (buffer: Buffer) => void) => { - const buffer = Buffer.alloc(size) - onWrite(buffer) - return buffer -} diff --git a/src/flac-tagger/index.ts b/src/flac-tagger/index.ts deleted file mode 100644 index d8233b5..0000000 --- a/src/flac-tagger/index.ts +++ /dev/null @@ -1,93 +0,0 @@ -import { VorbisComment, VorbisCommentBlock } from './metadata-block/vorbis-comment' -import { PictureBlock, PictureType } from './metadata-block/picture' -import { readFileSync, writeFileSync } from 'fs' -import { FlacStream } from './stream' -import { MetadataBlockType } from './metadata-block/header' - -export interface FlacTags { - vorbisComments: VorbisComment[] - picture?: { - pictureType?: PictureType - mime?: string - description?: string - colorDepth?: number - colors?: number - buffer: Buffer - } -} -export { FlacStream } from './stream' -export { BufferBase } from './buffer-base' -export { MetadataBlockType, MetadataBlockHeaderLength, MetadataBlockHeader } from './metadata-block/header' -export { MetadataBlock } from './metadata-block' -export { OtherMetadataBlock } from './metadata-block/other' -export { PictureBlock, PictureType } from './metadata-block/picture' -export { VorbisComment, VorbisCommentBlock } from './metadata-block/vorbis-comment' - -export const readFlacTags = (input: string | Buffer) => { - let buffer: Buffer - if (typeof input === 'string') { - buffer = readFileSync(input) - } else { - buffer = input - } - - const stream = FlacStream.fromBuffer(buffer) - const { vorbisCommentBlock, pictureBlock } = stream - const tags: FlacTags = { - vorbisComments: vorbisCommentBlock?.commentList ?? [], - picture: pictureBlock ? ({ - pictureType: pictureBlock.pictureType, - mime: pictureBlock.mime, - description: pictureBlock.description, - colorDepth: pictureBlock.colorDepth, - colors: pictureBlock.colors, - buffer: pictureBlock.pictureBuffer, - }) : undefined, - } - return tags -} - -export const writeFlacTags = (tags: FlacTags, filePath: string) => { - const buffer = readFileSync(filePath) - const stream = FlacStream.fromBuffer(buffer) - const originalLength = stream.length - - if (stream.vorbisCommentBlock) { - stream.vorbisCommentBlock.commentList = tags.vorbisComments - } else { - stream.metadataBlocks.push(new VorbisCommentBlock({ - commentList: tags.vorbisComments - })) - } - - if (tags.picture) { - const { pictureBlock } = stream - if (pictureBlock) { - stream.metadataBlocks = stream.metadataBlocks.filter(b => b !== pictureBlock) - } - - stream.metadataBlocks.push(new PictureBlock({ - pictureBuffer: tags.picture.buffer, - mime: tags.picture.mime, - description: tags.picture.description, - colorDepth: tags.picture.colorDepth, - colors: tags.picture.colors, - })) - } - - stream.metadataBlocks = stream.metadataBlocks.filter(b => b.type !== MetadataBlockType.Padding) - // if (stream.metadataBlocks.some(b => b.type === MetadataBlockType.Padding)) { - // stream.metadataBlocks = stream.metadataBlocks.filter(b => b.type !== MetadataBlockType.Padding) - // const paddingLength = originalLength - stream.length - // if (paddingLength - MetadataBlockHeaderLength > 0) { - // stream.metadataBlocks.push(new OtherMetadataBlock({ - // header: new MetadataBlockHeader({ - // type: MetadataBlockType.Padding, - // }), - // data: Buffer.alloc(paddingLength - MetadataBlockHeaderLength), - // })) - // } - // } - - writeFileSync(filePath, stream.toBuffer()) -} diff --git a/src/flac-tagger/metadata-block/header.ts b/src/flac-tagger/metadata-block/header.ts deleted file mode 100644 index 3683321..0000000 --- a/src/flac-tagger/metadata-block/header.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { BufferBase, allocBufferAndWrite } from '../buffer-base' - -export enum MetadataBlockType { - StreamInfo = 0, - Padding, - Application, - SeekTable, - VorbisComment, - CueSheet, - Picture, - Invalid = 127, -} -export const MetadataBlockHeaderLength = 4 -export class MetadataBlockHeader extends BufferBase { - isLast: boolean - type: MetadataBlockType - dataLength: number - - constructor(initialValues: { isLast?: boolean; type: MetadataBlockType; dataLength?: number }) { - super() - const { isLast = false, type, dataLength = 0 } = initialValues - this.isLast = isLast - this.type = type - this.dataLength = dataLength - } - - static fromBuffer(buffer: Buffer) { - const lastAndType = buffer.readUint8() - return new MetadataBlockHeader({ - isLast: (lastAndType & 0b10000000) === 1, - type: (lastAndType & 0b01111111) as MetadataBlockType, - dataLength: buffer.readUintBE(1, 3), - }) - } - - toBuffer() { - return allocBufferAndWrite(this.length, buffer => { - buffer.writeUint8(this.type + (this.isLast ? 0b10000000 : 0)) - buffer.writeUintBE(this.dataLength, 1, 3) - }) - } - - get length() { - return MetadataBlockHeaderLength - } -} diff --git a/src/flac-tagger/metadata-block/index.ts b/src/flac-tagger/metadata-block/index.ts deleted file mode 100644 index b35e317..0000000 --- a/src/flac-tagger/metadata-block/index.ts +++ /dev/null @@ -1,9 +0,0 @@ -import { BufferBase } from '../buffer-base' -import type { MetadataBlockHeader } from './header' - -export abstract class MetadataBlock extends BufferBase { - abstract header: MetadataBlockHeader - get type() { - return this.header.type - } -} diff --git a/src/flac-tagger/metadata-block/other.ts b/src/flac-tagger/metadata-block/other.ts deleted file mode 100644 index 342b5aa..0000000 --- a/src/flac-tagger/metadata-block/other.ts +++ /dev/null @@ -1,28 +0,0 @@ -import { MetadataBlock } from '.' -import { MetadataBlockHeader } from './header' - -export class OtherMetadataBlock extends MetadataBlock { - header: MetadataBlockHeader - data: Buffer - - constructor(initialValues: { header: MetadataBlockHeader; data: Buffer }) { - super() - Object.assign(this, initialValues) - } - - static fromBuffer(buffer: Buffer) { - const header = MetadataBlockHeader.fromBuffer(buffer) - return new OtherMetadataBlock({ - header, - data: buffer.slice(header.length, header.length + header.dataLength) - }) - } - - toBuffer() { - return Buffer.concat([this.header.toBuffer(), this.data]) - } - - get length() { - return this.header.length + this.data.length - } -} diff --git a/src/flac-tagger/metadata-block/parse.ts b/src/flac-tagger/metadata-block/parse.ts deleted file mode 100644 index e11a5a4..0000000 --- a/src/flac-tagger/metadata-block/parse.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { MetadataBlockType } from './header' -import { OtherMetadataBlock } from './other' -import { PictureBlock } from './picture' -import { VorbisCommentBlock } from './vorbis-comment' - -export const parseBlock = (buffer: Buffer) => { - const blockType = (buffer.readUint8() & 0b01111111) as MetadataBlockType - switch (blockType) { - case MetadataBlockType.VorbisComment: - return VorbisCommentBlock.fromBuffer(buffer) - case MetadataBlockType.Picture: - return PictureBlock.fromBuffer(buffer) - default: - return OtherMetadataBlock.fromBuffer(buffer) - } -} diff --git a/src/flac-tagger/metadata-block/picture.ts b/src/flac-tagger/metadata-block/picture.ts deleted file mode 100644 index 91381b7..0000000 --- a/src/flac-tagger/metadata-block/picture.ts +++ /dev/null @@ -1,161 +0,0 @@ -import { MetadataBlockHeader, MetadataBlockHeaderLength, MetadataBlockType } from './header' -import { MetadataBlock } from '.' -import imageinfo from 'imageinfo' -import { allocBufferAndWrite } from '../buffer-base' - -export enum PictureType { - Other, - FileIcon, - OtherFileIcon, - FrontCover, - BackCover, - LeafletPage, - Media, - LeadArtist, - Artist, - Conductor, - Band, - Composer, - Lyricist, - RecordingLocation, - DuringRecording, - DuringPerformance, - MovieScreenCapture, - ABrightColouredFish, - Illustration, - BandLogotype, - PublisherLogotype, -} -export class PictureBlock extends MetadataBlock { - header: MetadataBlockHeader - pictureType: PictureType - mime: string - description: string - width: number - height: number - colorDepth: number - colors: number - pictureBuffer: Buffer - - constructor(initialValues: { - header?: MetadataBlockHeader - pictureType?: PictureType - mime?: string - description?: string - width?: number - height?: number - colorDepth?: number - colors?: number - pictureBuffer: Buffer - }) { - super() - const { - header = new MetadataBlockHeader({ - type: MetadataBlockType.Picture, - }), - pictureType = PictureType.FrontCover, - mime, - description = '', - width, - height, - colorDepth = 24, - colors = 0, - pictureBuffer, - } = initialValues - this.header = header - this.pictureType = pictureType - if (mime && width && height) { - this.mime = mime - this.width = width - this.height = height - } else { - const info = imageinfo(pictureBuffer) - this.mime = mime ?? info.mimeType - this.width = width ?? info.width - this.height = height ?? info.height - } - this.description = description - this.colorDepth = colorDepth - this.colors = colors - this.pictureBuffer = pictureBuffer - } - - static fromBuffer(buffer: Buffer) { - let bufferIndex = 0 - - const header = MetadataBlockHeader.fromBuffer(buffer) - bufferIndex += header.length - - const pictureType = buffer.readUintBE(bufferIndex, 4) as PictureType - bufferIndex += 4 - - const mimeLength = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const mime = buffer.slice(bufferIndex, bufferIndex + mimeLength).toString() - bufferIndex += mimeLength - - const descriptionLength = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const description = buffer.slice(bufferIndex, bufferIndex + descriptionLength).toString() - bufferIndex += descriptionLength - - const width = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const height = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const colorDepth = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const colors = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const pictureDataLength = buffer.readUintBE(bufferIndex, 4) - bufferIndex += 4 - - const pictureBuffer = buffer.slice(bufferIndex, bufferIndex + pictureDataLength) - - return new PictureBlock({ - header, - pictureType, - mime, - description, - width, - height, - colorDepth, - colors, - pictureBuffer, - }) - } - - toBuffer() { - return Buffer.concat([ - this.header.toBuffer(), - allocBufferAndWrite(4, b => b.writeUint32BE(this.pictureType)), - allocBufferAndWrite(4, b => b.writeUint32BE(Buffer.byteLength(this.mime))), - Buffer.from(this.mime), - allocBufferAndWrite(4, b => b.writeUint32BE(Buffer.byteLength(this.description))), - Buffer.from(this.description), - allocBufferAndWrite(4, b => b.writeUint32BE(this.width)), - allocBufferAndWrite(4, b => b.writeUint32BE(this.height)), - allocBufferAndWrite(4, b => b.writeUint32BE(this.colorDepth)), - allocBufferAndWrite(4, b => b.writeUint32BE(this.colors)), - allocBufferAndWrite(4, b => b.writeUint32BE(this.pictureBuffer.length)), - this.pictureBuffer, - ]) - } - - get length() { - return MetadataBlockHeaderLength - + 4 // type length - + 4 // mime length - + Buffer.byteLength(this.mime) - + 4 // description length - + Buffer.byteLength(this.description) - + 4 * 5 // width, height, color depth, colors, picture data length - + this.pictureBuffer.length - } -} diff --git a/src/flac-tagger/metadata-block/vorbis-comment.ts b/src/flac-tagger/metadata-block/vorbis-comment.ts deleted file mode 100644 index 46f5749..0000000 --- a/src/flac-tagger/metadata-block/vorbis-comment.ts +++ /dev/null @@ -1,100 +0,0 @@ -import { MetadataBlockHeader, MetadataBlockHeaderLength, MetadataBlockType } from './header' -import { MetadataBlock } from '.' -import { allocBufferAndWrite } from '../buffer-base' - -export const DefaultVendorString = 'reference flac-tagger 1.0.0 20230626' -export type VorbisComment = string -export class VorbisCommentBlock extends MetadataBlock { - header: MetadataBlockHeader - vendorString: string - commentList: VorbisComment[] - - constructor( - initialValues: { - header?: MetadataBlockHeader - vendorString?: string - commentList?: VorbisComment[] - } = {}, - ) { - super() - const { - header = new MetadataBlockHeader({ - type: MetadataBlockType.VorbisComment, - }), - vendorString = DefaultVendorString, - commentList = [], - } = initialValues - this.header = header - this.vendorString = vendorString - this.commentList = commentList - } - - static fromBuffer(buffer: Buffer) { - let bufferIndex = 0 - - const header = MetadataBlockHeader.fromBuffer(buffer) - bufferIndex += header.length - - const vendorLength = buffer.readUintLE(bufferIndex, 4) - bufferIndex += 4 - - const vendorString = buffer.slice(bufferIndex, bufferIndex + vendorLength).toString() - bufferIndex += vendorLength - - const list: VorbisComment[] = [] - const listLength = buffer.readUintLE(bufferIndex, 4) - bufferIndex += 4 - - for (let commentIndex = 0; commentIndex < listLength; commentIndex++) { - const commentLength = buffer.readUintLE(bufferIndex, 4) - bufferIndex += 4 - - const comment = buffer.slice(bufferIndex, bufferIndex + commentLength).toString() - bufferIndex += commentLength - - list.push(comment) - } - return new VorbisCommentBlock({ - header, - vendorString, - commentList: list, - }) - } - - toBuffer() { - const commentBuffer = Buffer.alloc(this.commentListLength) - let commentBufferIndex = 0 - this.commentList.forEach(comment => { - const length = Buffer.byteLength(comment) - commentBuffer.writeUintLE(length, commentBufferIndex, 4) - commentBufferIndex += 4 - commentBuffer.write(comment, commentBufferIndex) - commentBufferIndex += length - }) - const vendorStringBuffer = Buffer.from(this.vendorString) - - return Buffer.concat([ - this.header.toBuffer(), - allocBufferAndWrite(4, b => b.writeUint32LE(vendorStringBuffer.length)), - vendorStringBuffer, - allocBufferAndWrite(4, b => b.writeUint32LE(this.commentList.length)), - commentBuffer, - ]) - } - - get commentListLength() { - return this.commentList - .map(it => Buffer.byteLength(it) + 4) - .reduce((previous, current) => previous + current, 0) - } - - get length() { - return ( - MetadataBlockHeaderLength + - 4 + - Buffer.byteLength(this.vendorString) + - 4 + - this.commentListLength - ) - } -} diff --git a/src/flac-tagger/stream.ts b/src/flac-tagger/stream.ts deleted file mode 100644 index ee73465..0000000 --- a/src/flac-tagger/stream.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { BufferBase } from './buffer-base' -import { MetadataBlock } from './metadata-block' -import { MetadataBlockType } from './metadata-block/header' -import { parseBlock } from './metadata-block/parse' -import type { PictureBlock } from './metadata-block/picture' -import type { VorbisCommentBlock } from './metadata-block/vorbis-comment' - -const FlacStreamMarker = 'fLaC' -// spec: https://xiph.org/flac/format.html -export class FlacStream extends BufferBase { - metadataBlocks: MetadataBlock[] - frameData: Buffer - - constructor(initialValues: { metadataBlocks: MetadataBlock[]; frameData: Buffer }) { - super() - Object.assign(this, initialValues) - } - - get vorbisCommentBlock(): VorbisCommentBlock | undefined { - return this.metadataBlocks.find(it => it.type === MetadataBlockType.VorbisComment) as VorbisCommentBlock - } - - get pictureBlock(): PictureBlock | undefined { - return this.metadataBlocks.find(it => it.type === MetadataBlockType.Picture) as PictureBlock - } - - static fromBuffer(buffer: Buffer) { - const marker = buffer.slice(0, 4).toString() - if (marker !== FlacStreamMarker) { - throw new Error('Invalid stream header') - } - let bufferIndex = 4 - const blocks: MetadataBlock[] = [] - const isNotLastBlock = () => blocks.length > 0 ? !(blocks[blocks.length - 1].header.isLast) : true - while (isNotLastBlock()) { - const restBlock = buffer.slice(bufferIndex) - const block = parseBlock(restBlock) - if (block.type === MetadataBlockType.Invalid) { - break - } - blocks.push(block) - bufferIndex += block.length - } - return new FlacStream({ - metadataBlocks: blocks, - frameData: buffer.slice(bufferIndex), - }) - } - - toBuffer() { - return Buffer.concat([ - Buffer.from(FlacStreamMarker), - ...this.metadataBlocks.map((block, index) => { - const isLast = index === this.metadataBlocks.length - 1 - block.header.isLast = isLast - block.header.dataLength = block.length - block.header.length - return block.toBuffer() - }), - this.frameData, - ]) - } - - get length() { - return this.metadataBlocks.map(it => it.length).reduce((previous, current) => previous + current, 0) - + this.frameData.length - } -} \ No newline at end of file