Apply new code format standard (#217)

This commit is contained in:
Chenlei Hu
2024-07-25 10:10:18 -04:00
committed by GitHub
parent 19c70d95d3
commit e179f75387
121 changed files with 11898 additions and 11983 deletions

View File

@@ -1,76 +1,76 @@
export function getFromFlacBuffer(buffer: ArrayBuffer): Record<string, string> {
const dataView = new DataView(buffer);
const dataView = new DataView(buffer)
// Verify the FLAC signature
const signature = String.fromCharCode(...new Uint8Array(buffer, 0, 4));
if (signature !== "fLaC") {
console.error("Not a valid FLAC file");
return;
const signature = String.fromCharCode(...new Uint8Array(buffer, 0, 4))
if (signature !== 'fLaC') {
console.error('Not a valid FLAC file')
return
}
// Parse metadata blocks
let offset = 4;
let vorbisComment = null;
let offset = 4
let vorbisComment = null
while (offset < dataView.byteLength) {
const isLastBlock = dataView.getUint8(offset) & 0x80;
const blockType = dataView.getUint8(offset) & 0x7f;
const blockSize = dataView.getUint32(offset, false) & 0xffffff;
offset += 4;
const isLastBlock = dataView.getUint8(offset) & 0x80
const blockType = dataView.getUint8(offset) & 0x7f
const blockSize = dataView.getUint32(offset, false) & 0xffffff
offset += 4
if (blockType === 4) {
// Vorbis Comment block type
vorbisComment = parseVorbisComment(
new DataView(buffer, offset, blockSize)
);
)
}
offset += blockSize;
if (isLastBlock) break;
offset += blockSize
if (isLastBlock) break
}
return vorbisComment;
return vorbisComment
}
export function getFromFlacFile(file: File): Promise<Record<string, string>> {
return new Promise((r) => {
const reader = new FileReader();
const reader = new FileReader()
reader.onload = function (event) {
const arrayBuffer = event.target.result as ArrayBuffer;
r(getFromFlacBuffer(arrayBuffer));
};
reader.readAsArrayBuffer(file);
});
const arrayBuffer = event.target.result as ArrayBuffer
r(getFromFlacBuffer(arrayBuffer))
}
reader.readAsArrayBuffer(file)
})
}
// Function to parse the Vorbis Comment block
function parseVorbisComment(dataView: DataView): Record<string, string> {
let offset = 0;
const vendorLength = dataView.getUint32(offset, true);
offset += 4;
const vendorString = getString(dataView, offset, vendorLength);
offset += vendorLength;
let offset = 0
const vendorLength = dataView.getUint32(offset, true)
offset += 4
const vendorString = getString(dataView, offset, vendorLength)
offset += vendorLength
const userCommentListLength = dataView.getUint32(offset, true);
offset += 4;
const comments = {};
const userCommentListLength = dataView.getUint32(offset, true)
offset += 4
const comments = {}
for (let i = 0; i < userCommentListLength; i++) {
const commentLength = dataView.getUint32(offset, true);
offset += 4;
const comment = getString(dataView, offset, commentLength);
offset += commentLength;
const commentLength = dataView.getUint32(offset, true)
offset += 4
const comment = getString(dataView, offset, commentLength)
offset += commentLength
const [key, value] = comment.split("=");
const [key, value] = comment.split('=')
comments[key] = value;
comments[key] = value
}
return comments;
return comments
}
function getString(dataView: DataView, offset: number, length: number): string {
let string = "";
let string = ''
for (let i = 0; i < length; i++) {
string += String.fromCharCode(dataView.getUint8(offset + i));
string += String.fromCharCode(dataView.getUint8(offset + i))
}
return string;
return string
}

View File

@@ -1,53 +1,53 @@
export function getFromPngBuffer(buffer: ArrayBuffer) {
// Get the PNG data as a Uint8Array
const pngData = new Uint8Array(buffer);
const dataView = new DataView(pngData.buffer);
const pngData = new Uint8Array(buffer)
const dataView = new DataView(pngData.buffer)
// Check that the PNG signature is present
if (dataView.getUint32(0) !== 0x89504e47) {
console.error("Not a valid PNG file");
return;
console.error('Not a valid PNG file')
return
}
// Start searching for chunks after the PNG signature
let offset = 8;
let txt_chunks: Record<string, string> = {};
let offset = 8
let txt_chunks: Record<string, string> = {}
// Loop through the chunks in the PNG file
while (offset < pngData.length) {
// Get the length of the chunk
const length = dataView.getUint32(offset);
const length = dataView.getUint32(offset)
// Get the chunk type
const type = String.fromCharCode(...pngData.slice(offset + 4, offset + 8));
if (type === "tEXt" || type == "comf" || type === "iTXt") {
const type = String.fromCharCode(...pngData.slice(offset + 4, offset + 8))
if (type === 'tEXt' || type == 'comf' || type === 'iTXt') {
// Get the keyword
let keyword_end = offset + 8;
let keyword_end = offset + 8
while (pngData[keyword_end] !== 0) {
keyword_end++;
keyword_end++
}
const keyword = String.fromCharCode(
...pngData.slice(offset + 8, keyword_end)
);
)
// Get the text
const contentArraySegment = pngData.slice(
keyword_end + 1,
offset + 8 + length
);
const contentJson = new TextDecoder("utf-8").decode(contentArraySegment);
txt_chunks[keyword] = contentJson;
)
const contentJson = new TextDecoder('utf-8').decode(contentArraySegment)
txt_chunks[keyword] = contentJson
}
offset += 12 + length;
offset += 12 + length
}
return txt_chunks;
return txt_chunks
}
export function getFromPngFile(file: File) {
return new Promise<Record<string, string>>((r) => {
const reader = new FileReader();
const reader = new FileReader()
reader.onload = (event) => {
r(getFromPngBuffer(event.target.result as ArrayBuffer));
};
r(getFromPngBuffer(event.target.result as ArrayBuffer))
}
reader.readAsArrayBuffer(file);
});
reader.readAsArrayBuffer(file)
})
}