refactor(server): calculate asset type server side (#3200)

* refactor(server): calculate asset type server-side

* chore: open api

* chore: remove comments

* fix: linting

* update

* Revert "update"

This reverts commit dc58702923250b9385d22468a7afe77dc9972a03.

* fix: upload LivePhotos

* chore: remove unused request fields for upload

* remove unused method

* mobile-fix: livePhoto filename

* fix: revert check for livephotos filename and extension

---------

Co-authored-by: Alex Tran <alex.tran1502@gmail.com>
This commit is contained in:
Jason Rasmussen
2023-07-11 23:56:30 -04:00
committed by GitHub
parent 93462aafbc
commit b71d7e33bb
24 changed files with 97 additions and 580 deletions

View File

@@ -5148,9 +5148,6 @@
"CreateAssetDto": {
"type": "object",
"properties": {
"assetType": {
"$ref": "#/components/schemas/AssetTypeEnum"
},
"assetData": {
"type": "string",
"format": "binary"
@@ -5167,9 +5164,6 @@
"type": "boolean",
"default": false
},
"fileExtension": {
"type": "string"
},
"deviceAssetId": {
"type": "string"
},
@@ -5198,9 +5192,7 @@
}
},
"required": [
"assetType",
"assetData",
"fileExtension",
"deviceAssetId",
"deviceId",
"fileCreatedAt",
@@ -5571,9 +5563,6 @@
"ImportAssetDto": {
"type": "object",
"properties": {
"assetType": {
"$ref": "#/components/schemas/AssetTypeEnum"
},
"isReadOnly": {
"type": "boolean",
"default": true
@@ -5612,7 +5601,6 @@
}
},
"required": [
"assetType",
"assetPath",
"deviceAssetId",
"deviceId",

View File

@@ -1,3 +1,4 @@
import { AssetType } from '@app/infra/entities';
import { BadRequestException } from '@nestjs/common';
import { extname } from 'node:path';
import pkg from 'src/../../package.json';
@@ -91,6 +92,8 @@ const sidecar: Record<string, string> = {
const isType = (filename: string, lookup: Record<string, string>) => !!lookup[extname(filename).toLowerCase()];
const getType = (filename: string, lookup: Record<string, string>) => lookup[extname(filename).toLowerCase()];
const lookup = (filename: string) =>
getType(filename, { ...image, ...video, ...sidecar }) || 'application/octet-stream';
export const mimeTypes = {
image,
@@ -102,5 +105,16 @@ export const mimeTypes = {
isProfile: (filename: string) => isType(filename, profile),
isSidecar: (filename: string) => isType(filename, sidecar),
isVideo: (filename: string) => isType(filename, video),
lookup: (filename: string) => getType(filename, { ...image, ...video, ...sidecar }) || 'application/octet-stream',
lookup,
assetType: (filename: string) => {
const contentType = lookup(filename).split('/')[0];
switch (contentType) {
case 'image':
return AssetType.IMAGE;
case 'video':
return AssetType.VIDEO;
default:
return AssetType.OTHER;
}
},
};

View File

@@ -1,4 +1,4 @@
import { AuthUserDto, IJobRepository, JobName, UploadFile } from '@app/domain';
import { AuthUserDto, IJobRepository, JobName, mimeTypes, UploadFile } from '@app/domain';
import { AssetEntity, UserEntity } from '@app/infra/entities';
import { parse } from 'node:path';
import { IAssetRepository } from './asset-repository';
@@ -26,7 +26,7 @@ export class AssetCore {
fileCreatedAt: dto.fileCreatedAt,
fileModifiedAt: dto.fileModifiedAt,
type: dto.assetType,
type: mimeTypes.assetType(file.originalPath),
isFavorite: dto.isFavorite,
isArchived: dto.isArchived ?? false,
duration: dto.duration || null,

View File

@@ -32,7 +32,6 @@ const _getCreateAssetDto = (): CreateAssetDto => {
const createAssetDto = new CreateAssetDto();
createAssetDto.deviceAssetId = 'deviceAssetId';
createAssetDto.deviceId = 'deviceId';
createAssetDto.assetType = AssetType.OTHER;
createAssetDto.fileCreatedAt = new Date('2022-06-19T23:41:36.910Z');
createAssetDto.fileModifiedAt = new Date('2022-06-19T23:41:36.910Z');
createAssetDto.isFavorite = false;

View File

@@ -1,8 +1,7 @@
import { toBoolean, toSanitized, UploadFieldName } from '@app/domain';
import { AssetType } from '@app/infra/entities';
import { ApiProperty } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional, IsString } from 'class-validator';
import { IsBoolean, IsNotEmpty, IsOptional, IsString } from 'class-validator';
export class CreateAssetBase {
@IsNotEmpty()
@@ -11,11 +10,6 @@ export class CreateAssetBase {
@IsNotEmpty()
deviceId!: string;
@IsNotEmpty()
@IsEnum(AssetType)
@ApiProperty({ enumName: 'AssetTypeEnum', enum: AssetType })
assetType!: AssetType;
@IsNotEmpty()
fileCreatedAt!: Date;
@@ -43,9 +37,6 @@ export class CreateAssetDto extends CreateAssetBase {
@Transform(toBoolean)
isReadOnly?: boolean = false;
@IsNotEmpty()
fileExtension!: string;
// The properties below are added to correctly generate the API docs
// and client SDKs. Validation should be handled in the controller.
@ApiProperty({ type: 'string', format: 'binary' })