imageManager.ts 1.2 KB

123456789101112131415161718192021222324252627282930313233343536
  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/files/${id}/download`
  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. 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