Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(server): more asset upload validation and docs #1720

Merged
merged 8 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions mobile/openapi/doc/AssetApi.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

87 changes: 84 additions & 3 deletions mobile/openapi/lib/api/asset_api.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mobile/openapi/test/asset_api_test.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions server/apps/immich/src/api-v1/asset/asset.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
UploadedFiles,
Patch,
StreamableFile,
ParseFilePipe,
} from '@nestjs/common';
import { Authenticated } from '../../decorators/authenticated.decorator';
import { AssetService } from './asset.service';
Expand All @@ -31,7 +32,6 @@ import { CuratedObjectsResponseDto } from './response-dto/curated-objects-respon
import { CuratedLocationsResponseDto } from './response-dto/curated-locations-response.dto';
import { AssetResponseDto, ImmichReadStream } from '@app/domain';
import { CheckDuplicateAssetResponseDto } from './response-dto/check-duplicate-asset-response.dto';
import { AssetFileUploadDto } from './dto/asset-file-upload.dto';
import { CreateAssetDto, mapToUploadFile } from './dto/create-asset.dto';
import { AssetFileUploadResponseDto } from './response-dto/asset-file-upload-response.dto';
import { DeleteAssetResponseDto } from './response-dto/delete-asset-response.dto';
Expand All @@ -55,6 +55,7 @@ import { SharedLinkResponseDto } from '@app/domain';
import { UpdateAssetsToSharedLinkDto } from './dto/add-assets-to-shared-link.dto';
import { AssetSearchDto } from './dto/asset-search.dto';
import { assetUploadOption, ImmichFile } from '../../config/asset-upload.config';
import FileNotEmptyValidator from '../validation/file-not-empty-validator';

function asStreamableFile({ stream, type, length }: ImmichReadStream) {
return new StreamableFile(stream, { type, length });
Expand All @@ -80,12 +81,13 @@ export class AssetController {
@ApiConsumes('multipart/form-data')
@ApiBody({
description: 'Asset Upload Information',
type: AssetFileUploadDto,
type: CreateAssetDto,
})
async uploadFile(
@GetAuthUser() authUser: AuthUserDto,
@UploadedFiles() files: { assetData: ImmichFile[]; livePhotoData?: ImmichFile[] },
@Body(ValidationPipe) dto: CreateAssetDto,
@UploadedFiles(new ParseFilePipe({ validators: [new FileNotEmptyValidator(['assetData'])] }))
files: { assetData: ImmichFile[]; livePhotoData?: ImmichFile[] },
@Body(new ValidationPipe()) dto: CreateAssetDto,
@Response({ passthrough: true }) res: Res,
): Promise<AssetFileUploadResponseDto> {
const file = mapToUploadFile(files.assetData[0]);
Expand Down

This file was deleted.

11 changes: 10 additions & 1 deletion server/apps/immich/src/api-v1/asset/dto/create-asset.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AssetType } from '@app/infra';
import { ApiProperty } from '@nestjs/swagger';
import { IsBoolean, IsNotEmpty, IsOptional } from 'class-validator';
import { IsBoolean, IsEnum, IsNotEmpty, IsOptional } from 'class-validator';
import { ImmichFile } from '../../../config/asset-upload.config';

export class CreateAssetDto {
Expand All @@ -11,6 +11,7 @@ export class CreateAssetDto {
deviceId!: string;

@IsNotEmpty()
@IsEnum(AssetType)
@ApiProperty({ enumName: 'AssetTypeEnum', enum: AssetType })
assetType!: AssetType;

Expand All @@ -32,6 +33,14 @@ export class CreateAssetDto {

@IsOptional()
duration?: 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' })
jrasm91 marked this conversation as resolved.
Show resolved Hide resolved
assetData!: any;

@ApiProperty({ type: 'string', format: 'binary' })
livePhotoData?: any;
}

export interface UploadFile {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { FileValidator, Injectable } from '@nestjs/common';

@Injectable()
export default class FileNotEmptyValidator extends FileValidator {
requiredFields: string[];

constructor(requiredFields: string[]) {
super({});
this.requiredFields = requiredFields;
}

isValid(files?: any): boolean {
if (!files) {
return false;
}

return this.requiredFields.every((field) => {
return files[field];
});
}

buildErrorMessage(): string {
return `Field(s) ${this.requiredFields.join(', ')} should not be empty`;
}
}
12 changes: 8 additions & 4 deletions server/bin/generate-open-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

function mobile {
rm -rf ../mobile/openapi
cd ./openapi-generator/templates/serialization/native
cd ./openapi-generator/templates/mobile/serialization/native
wget -O native_class.mustache https://raw.githubusercontent.com/OpenAPITools/openapi-generator/master/modules/openapi-generator/src/main/resources/dart2/serialization/native/native_class.mustache
patch -u native_class.mustache <native_class.mustache.patch
cd ../../../..
npx openapi-generator-cli generate -g dart -i ./immich-openapi-specs.json -o ../mobile/openapi -t ./openapi-generator/templates
cd ../../../../..
npx openapi-generator-cli generate -g dart -i ./immich-openapi-specs.json -o ../mobile/openapi -t ./openapi-generator/templates/mobile

# Post generate patches
patch --no-backup-if-mismatch -u ../mobile/openapi/lib/api_client.dart <./openapi-generator/patch/api_client.dart.patch
Expand All @@ -15,7 +15,11 @@ function mobile {

function web {
rm -rf ../web/src/api/open-api
npx openapi-generator-cli generate -g typescript-axios -i ./immich-openapi-specs.json -o ../web/src/api/open-api
cd ./openapi-generator/templates/web
wget -O apiInner.mustache https://raw.githubusercontent.com/OpenAPITools/openapi-generator/v6.0.1/modules/openapi-generator/src/main/resources/typescript-axios/apiInner.mustache
patch -u apiInner.mustache < apiInner.mustache.patch
cd ../../..
npx openapi-generator-cli generate -g typescript-axios -i ./immich-openapi-specs.json -o ../web/src/api/open-api -t ./openapi-generator/templates/web
}

if [[ $1 == 'mobile' ]]; then
Expand Down
44 changes: 41 additions & 3 deletions server/immich-openapi-specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -1077,7 +1077,7 @@
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/AssetFileUploadDto"
"$ref": "#/components/schemas/CreateAssetDto"
}
}
}
Expand Down Expand Up @@ -3758,16 +3758,54 @@
"profileImagePath"
]
},
"AssetFileUploadDto": {
"CreateAssetDto": {
"type": "object",
"properties": {
"assetType": {
"$ref": "#/components/schemas/AssetTypeEnum"
},
"assetData": {
"type": "string",
"format": "binary"
},
"livePhotoData": {
"type": "string",
"format": "binary"
},
"deviceAssetId": {
"type": "string"
},
"deviceId": {
"type": "string"
},
"createdAt": {
"type": "string"
},
"modifiedAt": {
"type": "string"
},
"isFavorite": {
"type": "boolean"
},
"isVisible": {
"type": "boolean"
},
"fileExtension": {
"type": "string"
},
"duration": {
"type": "string"
}
},
"required": [
"assetData"
"assetType",
"assetData",
"deviceAssetId",
"deviceId",
"createdAt",
"modifiedAt",
"isFavorite",
"fileExtension"
]
},
"AssetFileUploadResponseDto": {
Expand Down
2 changes: 1 addition & 1 deletion server/openapi-generator/patch/api_client.dart.patch
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@
? json
- : _deserialize(jsonDecode(json), targetType, growable: growable);
+ : _deserialize(await compute((String j) => jsonDecode(j), json), targetType, growable: growable);
}
}
Loading