|
|
@@ -25,7 +25,7 @@ class ImageManager {
|
|
|
* @param size
|
|
|
* @param defaultImage The path of an image in the 'public' folder, default: '/images/default/picture.jpeg'
|
|
|
*/
|
|
|
- public async get(
|
|
|
+ public get(
|
|
|
id: number | string | null,
|
|
|
size: IMAGE_SIZE = IMAGE_SIZE.MD,
|
|
|
defaultImage: string | null = null,
|
|
|
@@ -33,7 +33,7 @@ class ImageManager {
|
|
|
const defaultUrl = defaultImage ?? ImageManager.defaultImage
|
|
|
|
|
|
if (id === null) {
|
|
|
- return defaultUrl
|
|
|
+ return Promise.resolve(defaultUrl)
|
|
|
}
|
|
|
|
|
|
const matches = id.toString().match(/\/api\/files\/(\d+)(?:\/\w+)?/)
|
|
|
@@ -45,17 +45,16 @@ class ImageManager {
|
|
|
}
|
|
|
|
|
|
if (!(typeof id === 'number' && Number.isInteger(id))) {
|
|
|
- throw new Error('Error: image ' + id + ' is invalid')
|
|
|
+ throw new TypeError('Error: image ' + id + ' is invalid')
|
|
|
}
|
|
|
|
|
|
try {
|
|
|
- return size === IMAGE_SIZE.RAW ?
|
|
|
- this.getRaw(id) :
|
|
|
- this.getProcessed(id, size)
|
|
|
-
|
|
|
+ return size === IMAGE_SIZE.RAW
|
|
|
+ ? this.getRaw(id)
|
|
|
+ : this.getProcessed(id, size)
|
|
|
} catch (error) {
|
|
|
console.error(error)
|
|
|
- return defaultUrl
|
|
|
+ return Promise.resolve(defaultUrl)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -67,20 +66,19 @@ class ImageManager {
|
|
|
*/
|
|
|
private async getProcessed(
|
|
|
id: number | null,
|
|
|
- size: IMAGE_SIZE = IMAGE_SIZE.MD
|
|
|
+ size: IMAGE_SIZE = IMAGE_SIZE.MD,
|
|
|
): Promise<string> {
|
|
|
-
|
|
|
- let imageUrl = `api/image/download/${id}/${size}`
|
|
|
+ const imageUrl = `api/image/download/${id}/${size}`
|
|
|
|
|
|
// Une image doit toujours avoir le time en options pour éviter les problèmes de cache
|
|
|
- const query: AssociativeArray = {0: this.getCacheKey()}
|
|
|
+ const query: AssociativeArray = { 0: this.getCacheKey() }
|
|
|
|
|
|
- const response = await this.apiRequestService.get(imageUrl, query);
|
|
|
+ const response = await this.apiRequestService.get(imageUrl, query)
|
|
|
|
|
|
const cachedImageUrl = response.toString()
|
|
|
|
|
|
if (!cachedImageUrl) {
|
|
|
- throw new Error('Error: image ' + id + ' not found');
|
|
|
+ throw new Error('Error: image ' + id + ' not found')
|
|
|
}
|
|
|
|
|
|
return UrlUtils.addQuery(cachedImageUrl, query)
|
|
|
@@ -94,7 +92,6 @@ class ImageManager {
|
|
|
* @private
|
|
|
*/
|
|
|
private async getRaw(id: number | null): Promise<string> {
|
|
|
-
|
|
|
const imageUrl = `api/file/download/${id}`
|
|
|
|
|
|
// Une image doit toujours avoir le time en options pour éviter les problèmes de cache
|
|
|
@@ -102,11 +99,11 @@ class ImageManager {
|
|
|
|
|
|
const blobPart = await this.apiRequestService.get(imageUrl, query)
|
|
|
if (!blobPart) {
|
|
|
- throw new Error('Error: image ' + id + ' not found');
|
|
|
+ throw new Error('Error: image ' + id + ' not found')
|
|
|
}
|
|
|
|
|
|
if (!(blobPart instanceof Blob) || blobPart.size === 0) {
|
|
|
- throw new Error('Error: image ' + id + ' is invalid');
|
|
|
+ throw new Error('Error: image ' + id + ' is invalid')
|
|
|
}
|
|
|
|
|
|
return await this.toBase64(blobPart)
|