imageManager.ts 1.2 KB

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