Download spectrum/api/utils/file-system.js from Devendra174/react-code-dataset: direct link, hf CLI and curl.
- Browser
- Download file 1.02 kB
-
https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/api/utils/file-system.js
- Command line
-
hf download hf://datasets/Devendra174/react-code-dataset/spectrum/api/utils/file-system.js
-
curl -L -o file-system.js https://huggingface.co/datasets/Devendra174/react-code-dataset/resolve/main/spectrum/api/utils/file-system.js
1.02 kB
| // @flow | |
| import uuidv4 from 'uuid/v4'; | |
| import fs from 'fs'; | |
| import type { FileUpload, EntityTypes } from 'shared/types'; | |
| const STORAGE_DIR = 'public/uploads'; | |
| const READ_WRITE_MODE = 0o777; | |
| const dirExists = (path: string): Promise<boolean> => | |
| new Promise(res => fs.access(path, fs.constants.F_OK, err => res(!!!err))); | |
| const createUploadsDir = (path: string): Promise<void> => | |
| new Promise(res => | |
| fs.mkdir(path, READ_WRITE_MODE, err => { | |
| if (err) throw new Error(err); | |
| res(); | |
| }) | |
| ); | |
| export const uploadImage = async ( | |
| file: FileUpload, | |
| entity: EntityTypes, | |
| id: string | |
| ): Promise<string> => { | |
| const result = await file; | |
| if (!(await dirExists(STORAGE_DIR))) { | |
| await createUploadsDir(STORAGE_DIR); | |
| } | |
| return new Promise(res => { | |
| const filePath = `${uuidv4()}-${entity}-${id}`; | |
| const { stream } = result; | |
| stream.pipe(fs.createWriteStream(`${STORAGE_DIR}/${filePath}`)); | |
| stream.on('end', () => { | |
| res(encodeURI(`/uploads/${filePath}`)); | |
| }); | |
| }); | |
| }; | |