Added Asset image handling
MODIFY
src/server/rest/CentralRestServerService.ts
src/server/rest/CentralRestServerService.ts
@@ -300,6 +300,9 @@ export default class CentralRestServerService {
case ServerAction.CAR_CATALOG_IMAGE:
await CarService.handleGetCarCatalogImage(action, req, res, next);
break;
+ case ServerAction.ASSET_IMAGE:
+ await AssetService.handleGetAssetImage(action, req, res, next);
+ break;
case ServerAction.COMPANY_LOGO:
await CompanyService.handleGetCompanyLogo(action, req, res, next);
break;

MODIFY
src/server/rest/v1/service/AssetService.ts
src/server/rest/v1/service/AssetService.ts
@@ -303,29 +303,26 @@ export default class AssetService {
}
public static async handleGetAssetImage(action: ServerAction, req: Request, res: Response, next: NextFunction): Promise<void> {
- // Check if component is active
- UtilsService.assertComponentIsActiveFromToken(req.user, TenantComponents.ASSET,
- Action.READ, Entity.ASSET, MODULE_NAME, 'handleGetAssetImage');
// Filter
- const assetID = AssetSecurity.filterAssetRequestByID(req.query);
- // Charge Box is mandatory
- UtilsService.assertIdIsProvided(action, assetID, MODULE_NAME, 'handleGetAssetImage', req.user);
- // Check auth
- if (!Authorizations.canReadAsset(req.user)) {
- throw new AppAuthError({
- errorCode: HTTPAuthError.ERROR,
- user: req.user,
- action: Action.READ, entity: Entity.ASSET,
- module: MODULE_NAME, method: 'handleGetAssetImage',
- value: assetID
- });
- }
+ const filteredRequest = AssetSecurity.filterAssetImageRequest(req.query);
+ UtilsService.assertIdIsProvided(action, filteredRequest.ID, MODULE_NAME, 'handleGetAssetImage', req.user);
// Get it
- const assetImage = await AssetStorage.getAssetImage(req.user.tenantID, assetID);
- // Check
- UtilsService.assertObjectExists(action, assetImage, `Asset with ID '${assetID}' does not exist`,
- MODULE_NAME, 'handleGetAssetImage', req.user);
- res.json({ id: assetImage.id, image: assetImage.image });
+ const assetImage = await AssetStorage.getAssetImage(filteredRequest.TenantID, filteredRequest.ID);
+ // Return
+ if (assetImage?.image) {
+ let header = 'image';
+ let encoding: BufferEncoding = 'base64';
+ // Remove encoding header
+ if (assetImage.image.startsWith('data:image/')) {
+ header = assetImage.image.substring(5, assetImage.image.indexOf(';'));
+ encoding = assetImage.image.substring(assetImage.image.indexOf(';') + 1, assetImage.image.indexOf(',')) as BufferEncoding;
+ assetImage.image = assetImage.image.substring(assetImage.image.indexOf(',') + 1);
+ }
+ res.setHeader('content-type', header);
+ res.send(assetImage.image ? Buffer.from(assetImage.image, encoding) : null);
+ } else {
+ res.send(null);
+ }
next();
}

MODIFY
src/server/rest/v1/service/security/AssetSecurity.ts
src/server/rest/v1/service/security/AssetSecurity.ts
-import { HttpAssetConsumptionRequest, HttpAssetRequest, HttpAssetsRequest } from '../../../../../types/requests/HttpAssetRequest';
+import { HttpAssetConsumptionRequest, HttpAssetImageRequest, HttpAssetRequest, HttpAssetsRequest } from '../../../../../types/requests/HttpAssetRequest';
import Asset from '../../../../../types/Asset';
import UtilsSecurity from './UtilsSecurity';
@@ -10,6 +10,13 @@ export default class AssetSecurity {
return sanitize(request.ID);
}
+ public static filterAssetImageRequest(request: any): HttpAssetImageRequest {
+ return {
+ ID: sanitize(request.ID),
+ TenantID: sanitize(request.TenantID),
+ };
+ }
+
public static filterAssetRequest(request: any): HttpAssetRequest {
return {
ID: sanitize(request.ID),

MODIFY
src/types/requests/HttpAssetRequest.ts
src/types/requests/HttpAssetRequest.ts
@@ -19,3 +19,7 @@ export interface HttpAssetConsumptionRequest {
StartDate: Date;
EndDate: Date;
}
+
+export interface HttpAssetImageRequest extends HttpByIDRequest {
+ TenantID: string;
+}

Added Asset image handling