| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105 |
- import ApiRequestService from './apiRequestService'
- import FileUtils from '~/services/utils/fileUtils'
- import { FILE_TYPE, FILE_VISIBILITY } from '~/types/enum/enums'
- /**
- * Permet le requêtage, l'upload et la manipulation des images via l'API Opentalent
- */
- class ImageManager {
- private apiRequestService: ApiRequestService
- public static readonly defaultImage = '/images/default/picture.jpeg'
- public constructor(apiRequestService: ApiRequestService) {
- this.apiRequestService = apiRequestService
- }
- /**
- * Retourne l'image correspondante sous forme d'un blob encodé au format base64,
- * ou l'url d'une image par défaut si l'image est introuvable ou si l'id passé en paramètre est null
- *
- * Attention, les dimensions (hauteur / largeur) ne s'appliqueront pas à l'image par défaut, il est nécessaire de
- * les redéfinir dans le composant lui-même.
- *
- * @param id The id of the image; if null, the url to the default image is returned
- * @param defaultImage The path of an image in the 'public' folder, default: '/images/default/picture.jpeg'
- * @param height Height of the image (does not apply to default image)
- * @param width Width of the image (does not apply to default image)
- */
- public async get(
- id: number | null,
- defaultImage: string | null = null,
- height: number = 0,
- width: number = 0,
- ): Promise<string | ArrayBuffer> {
- const defaultUrl = defaultImage ?? ImageManager.defaultImage
- if (id === null) {
- return defaultUrl
- }
- const imageUrl = `api/file/download/${id}`
- // Set requested size if needed
- if (height > 0 || width > 0) {
- // @see https://thumbor.readthedocs.io/en/latest/crop_and_resize_algorithms.html
- // TODO: ajouter le support de ces options dans ap2i
- // url = UrlUtils.join(url, `${height}x${width}`)
- }
- // Une image doit toujours avoir le time en options pour éviter les problèmes de cache
- const query = [this.getCacheKey()]
- const blobPart = await this.apiRequestService.get(imageUrl, query);
- if (!blobPart) {
- console.error('Error: image ' + id + ' not found');
- return defaultUrl;
- }
- if (!(blobPart instanceof Blob) || blobPart.size === 0) {
- console.error('Error: image ' + id + ' is invalid');
- return defaultUrl;
- }
- return await this.toBase64(blobPart)
- }
- public async upload(
- filename: string,
- content: string,
- visibility: string = FILE_VISIBILITY.NOBODY,
- config: string | null = null,
- ) {
- content = content.replace(/^data:image\/[\w/]+;base64,/, '')
- const data = JSON.stringify({
- filename,
- content,
- type: FILE_TYPE.UPLOADED,
- visibility,
- config,
- })
- return await this.apiRequestService.post('api/upload', data)
- }
- /**
- * Convert the API response into base64
- * @param data
- * @protected
- */
- protected async toBase64(data: BlobPart) {
- const blob = FileUtils.newBlob(data)
- return (await FileUtils.blobToBase64(blob)) ?? ''
- }
- /**
- * On passe cette clé en paramètres des requêtes pour éviter les problèmes de cache
- *
- * @protected
- */
- protected getCacheKey() {
- return new Date().getTime().toString()
- }
- }
- export default ImageManager
|