Skip to content

Instantly share code, notes, and snippets.

@fanch317
Last active February 10, 2022 07:50
Show Gist options
  • Save fanch317/a8b9e8fda7339dd642755a7e53ff7dfa to your computer and use it in GitHub Desktop.
Save fanch317/a8b9e8fda7339dd642755a7e53ff7dfa to your computer and use it in GitHub Desktop.
Snippets

Snippets JS /TS

Extraits

Tri naturel (a1, b0, b01, b1, b10...)

const arraySorted = arrayUnsorted.sort((a: Object, b: Object) => {
  return ("" + a.name).localeCompare("" + a.name.toString(), undefined, {
    numeric: true,
    sensitivity: "base",
  });
});

Intercepteur pour upload multipart

    @Post('upload/multiple/:type/:cat/:id')
    @UseInterceptors(FilesInterceptor('photos', 3, {
        storage: diskStorage({
            destination: (req, file, cb) => {
                const path = process.env.UPLOAD_FOLDER + `/${req.params.type}/${req.params.cat}/${req.params.id}/`;
                const exists = fs.existsSync(path);

                if (!exists) {
                    fs.mkdirSync(path, { recursive: true });
                }

                cb(null, path);
            },
            filename: function (req, file, cb) {
                /* Attention, pas de moyen d'utiliser un service dans un interceptor.
                Penser à mettre à jour la regex si elle change */
                cb(null, file.originalname.replace(/\s/gm, '_'));
            },
        })
    }))
    uploadFiles(
        @Param('type') profil: string,
        @Param('id') id: string,
        @UploadedFiles() files: Express.Multer.File[]
    ) {
        let response = new Array();

        for (let file of files) {
            response.push({
                name: this.utilsService.normalizeName(file.originalname),
                url: `${profil}/${id}/${this.utilsService.normalizeName(file.originalname)}`,
            });
        }

        return response;
    }

    @Public()
    @Get(':imgpath')
    getFile(@Param('imgpath') image, @Res() res) {
        return res.sendFile(process.env.UPLOAD_FOLDER + `/${image}`, { root: '/' });
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment