Download api/server/utils/files.js from Zhitomir/chat: direct link, hf CLI and curl.
- Browser
- Download file 1.47 kB
-
https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/server/utils/files.js
- Command line
-
hf download hf://spaces/Zhitomir/chat/api/server/utils/files.js
-
curl -L -o files.js https://huggingface.co/spaces/Zhitomir/chat/resolve/main/api/server/utils/files.js
1.47 kB
| const sharp = require('sharp'); | |
| /** | |
| * Determines the file type of a buffer | |
| * @param {Buffer} dataBuffer | |
| * @param {boolean} [returnFileType=false] - Optional. If true, returns the file type instead of the file extension. | |
| * @returns {Promise<string|null|import('file-type').FileTypeResult>} - Returns the file extension if found, else null | |
| * */ | |
| const determineFileType = async (dataBuffer, returnFileType) => { | |
| const fileType = await import('file-type'); | |
| const type = await fileType.fileTypeFromBuffer(dataBuffer); | |
| if (returnFileType) { | |
| return type; | |
| } | |
| return type ? type.ext : null; // Returns extension if found, else null | |
| }; | |
| /** | |
| * Get buffer metadata | |
| * @param {Buffer} buffer | |
| * @returns {Promise<{ bytes: number, type: string, dimensions: Record<string, number>, extension: string}>} | |
| */ | |
| const getBufferMetadata = async (buffer) => { | |
| const fileType = await determineFileType(buffer, true); | |
| const bytes = buffer.length; | |
| let extension = fileType ? fileType.ext : 'unknown'; | |
| /** @type {Record<string, number>} */ | |
| let dimensions = {}; | |
| if (fileType && fileType.mime.startsWith('image/') && extension !== 'unknown') { | |
| const imageMetadata = await sharp(buffer).metadata(); | |
| dimensions = { | |
| width: imageMetadata.width, | |
| height: imageMetadata.height, | |
| }; | |
| } | |
| return { | |
| bytes, | |
| type: fileType?.mime ?? 'unknown', | |
| dimensions, | |
| extension, | |
| }; | |
| }; | |
| module.exports = { determineFileType, getBufferMetadata }; | |