imageManager.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import ApiRequestService from "./apiRequestService";
  2. import ImageUtils from "~/services/utils/imageUtils";
  3. class ImageManager {
  4. private apiRequestService: ApiRequestService;
  5. public constructor(apiRequestService: ApiRequestService) {
  6. this.apiRequestService = apiRequestService
  7. }
  8. public async get(id: number, height: number = 0, width: number = 0) {
  9. let url = `api/download/${id}`
  10. // Set requested size if needed
  11. if (height > 0 || width > 0) {
  12. // @see https://thumbor.readthedocs.io/en/latest/crop_and_resize_algorithms.html
  13. // TODO: ajouter le support de ces options dans ap2i
  14. // url = Url.join(url, `${height}x${width}`)
  15. }
  16. // Une image doit toujours avoir le time en options pour éviter les problème de cache
  17. const query = [new Date().getTime().toString()]
  18. const response: any = await this.apiRequestService.get(url, query)
  19. if(!response.data || response.data.size === 0) {
  20. throw new Error('Error: image not found or invalid')
  21. }
  22. const blob = await ImageUtils.newBlob(response.data)
  23. return await ImageUtils.blobToBase64(blob);
  24. }
  25. }
  26. export default ImageManager