Skip to content

Commit 17e8cf7

Browse files
committed
fix exception at public file
1 parent 62f87cb commit 17e8cf7

File tree

3 files changed

+87
-84
lines changed

3 files changed

+87
-84
lines changed

app/controller/file.controller.ts

Lines changed: 49 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -12,70 +12,74 @@ import { FileUtil } from '../util/file.util';
1212
import { FileServiceWrapper } from '../wrapper/file-service.wrapper';
1313

1414
export class FileController {
15-
constructor(private fileUtil: FileUtil = new FileUtil()) {}
15+
constructor( private fileUtil: FileUtil = new FileUtil() ) {
16+
}
17+
1618
/**
1719
* creates file
1820
* @param context context
1921
* @param file file
2022
*/
21-
createFile = async (context: Context, file: File): Promise<any> => {
22-
if (!this.isValidFile(file))
23+
createFile = async ( context: Context, file: File ): Promise<any> => {
24+
if ( !this.isValidFile( file ) )
2325
throw new Error(
24-
'File must contain contentType, title, description and data'
26+
'File must contain contentType, title, description and data'
2527
);
2628

2729
const serviceClient = await this.getServiceClient(
28-
context.mongodb_provider,
29-
context.serviceKey
30+
context.mongodb_provider,
31+
context.serviceKey
3032
);
3133

3234
if (
33-
!(file.reporter && file.reporter.length > 0) &&
34-
(file.type === FileType.USER_PROFILE_PICTURE ||
35-
file.type === FileType.USER_COVER_PICTURE)
35+
!( file.reporter && file.reporter.length > 0 ) &&
36+
( file.type === FileType.USER_PROFILE_PICTURE ||
37+
file.type === FileType.USER_COVER_PICTURE )
3638
) {
3739
file.reporter = context.username;
3840
}
3941

40-
file = await serviceClient.service.upload(serviceClient.client, file);
42+
file = await serviceClient.service.upload( serviceClient.client, file );
4143

42-
if (file.uploaded && !this.fileUtil.isPublicFileType(file.type)) {
43-
const fileRepository = new FileRepository(context.postgresql_provider);
44+
if ( file.uploaded && !this.fileUtil.isPublicFileType( file.type ) ) {
45+
const fileRepository = new FileRepository( context.postgresql_provider );
4446
return fileRepository.saveFile(
45-
file.reporter || context.username,
46-
file,
47-
context.serviceKey
47+
file.reporter || context.username,
48+
file,
49+
context.serviceKey
4850
);
4951
}
5052

51-
throw new Error('File upload failed');
53+
if ( !this.fileUtil.isPublicFileType( file.type ) ) {
54+
throw new Error( 'File upload failed' );
55+
}
5256
};
5357

5458
/**
5559
* gets file by id
5660
* @param context context
5761
* @param id file id
5862
*/
59-
downloadFile = async (context: Context, id: any): Promise<File> => {
60-
const fileRepository = new FileRepository(context.postgresql_provider);
63+
downloadFile = async ( context: Context, id: any ): Promise<File> => {
64+
const fileRepository = new FileRepository( context.postgresql_provider );
6165

62-
const file = await fileRepository.getFile(context.username, id);
66+
const file = await fileRepository.getFile( context.username, id );
6367

64-
if (file === undefined) {
65-
throw new Error('File not found with id: ' + id);
68+
if ( file === undefined ) {
69+
throw new Error( 'File not found with id: ' + id );
6670
}
6771

6872
const serviceClient = await this.getServiceClient(
69-
context.mongodb_provider,
70-
file.service_key
73+
context.mongodb_provider,
74+
file.service_key
7175
);
7276

73-
if (file.is_public) {
77+
if ( file.is_public ) {
7478
file.url = serviceClient.publicUrl + file.external_file_id;
7579
} else {
7680
file.data = await serviceClient.service.download(
77-
serviceClient.client,
78-
file.external_file_id
81+
serviceClient.client,
82+
file.external_file_id
7983
);
8084
}
8185

@@ -88,17 +92,17 @@ export class FileController {
8892
* @param serviceKey service key
8993
*/
9094
private getServiceClient = async (
91-
provider: MongoDbProvider,
92-
serviceKey: string
95+
provider: MongoDbProvider,
96+
serviceKey: string
9397
): Promise<ServiceClient> => {
94-
const serviceConfig = await this.getServiceConfig(provider, serviceKey);
98+
const serviceConfig = await this.getServiceConfig( provider, serviceKey );
9599

96-
const service = new FileServiceWrapper(serviceConfig.payload.service);
100+
const service = new FileServiceWrapper( serviceConfig.payload.service );
97101

98-
const client = await service.initializeClient(serviceConfig);
102+
const client = await service.initializeClient( serviceConfig );
99103

100-
if (client === undefined)
101-
throw new Error('Client is not initialized correctly');
104+
if ( client === undefined )
105+
throw new Error( 'Client is not initialized correctly' );
102106

103107
return {
104108
client,
@@ -113,19 +117,19 @@ export class FileController {
113117
* @param serviceKey service key
114118
*/
115119
private getServiceConfig = async (
116-
provider: MongoDbProvider,
117-
serviceKey: string
120+
provider: MongoDbProvider,
121+
serviceKey: string
118122
): Promise<any> => {
119123
const conn = provider.getConnection();
120124

121125
const serviceProviderRepository =
122-
await new ServiceProviderRepository().initialize(conn);
126+
await new ServiceProviderRepository().initialize( conn );
123127

124128
let serviceConfig: any =
125-
await serviceProviderRepository.getServiceProviderByKey(serviceKey);
129+
await serviceProviderRepository.getServiceProviderByKey( serviceKey );
126130

127-
if (serviceConfig === null)
128-
throw new Error('Upload service can not be found');
131+
if ( serviceConfig === null )
132+
throw new Error( 'Upload service can not be found' );
129133

130134
return serviceConfig;
131135
};
@@ -134,13 +138,13 @@ export class FileController {
134138
* checks is file valid
135139
* @param file file
136140
*/
137-
private isValidFile = (file: File): boolean => {
141+
private isValidFile = ( file: File ): boolean => {
138142
if (
139-
file.title &&
140-
file.content_type &&
141-
file.data &&
142-
file.description &&
143-
file
143+
file.title &&
144+
file.content_type &&
145+
file.data &&
146+
file.description &&
147+
file
144148
) {
145149
return true;
146150
}
Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { v4 as uuidv4 } from 'uuid';
2-
import { FileType } from '../enum/file-type.enum';
32
import { FileService } from '../interface/file-service.interface';
43
import { File } from '../interface/file.interface';
54
import { FileUtil } from '../util/file.util';
@@ -9,11 +8,11 @@ class S3Package {
98
static S3: any;
109

1110
public static getInstance() {
12-
if (!this.config && !this.S3) {
13-
const { config } = require('aws-sdk/global');
11+
if ( !this.config && !this.S3 ) {
12+
const { config } = require( 'aws-sdk/global' );
1413
this.config = config;
15-
this.S3 = require('aws-sdk/clients/s3');
16-
console.info('Initializing S3 Package. Config: ', this.config);
14+
this.S3 = require( 'aws-sdk/clients/s3' );
15+
console.info( 'Initializing S3 Package. Config: ', this.config );
1716
}
1817

1918
return {
@@ -31,7 +30,7 @@ export class S3FileService implements FileService {
3130
* initializes client
3231
* @param providerConfig provider config
3332
*/
34-
async initializeClient(providerConfig: any): Promise<any> {
33+
async initializeClient( providerConfig: any ): Promise<any> {
3534
// Dynamically import aws sdks on initialize
3635
const s3Package: any = S3Package.getInstance();
3736
const config: any = s3Package.config;
@@ -41,48 +40,48 @@ export class S3FileService implements FileService {
4140

4241
this.payload = payload;
4342

44-
config.update({
43+
config.update( {
4544
accessKeyId: payload.accessKeyId,
4645
secretAccessKey: payload.secretAccessKey,
4746
region: payload.region,
48-
});
47+
} );
4948

50-
return new S3({
49+
return new S3( {
5150
apiVersion: payload.apiVersion,
52-
});
51+
} );
5352
}
5453

5554
/**
5655
* uploads file
5756
* @param client service client
5857
* @param file file
5958
*/
60-
async upload(client: any, file: File): Promise<File> {
61-
if (this.fileUtil.isPublicFileType(file.type)) {
59+
async upload( client: any, file: File ): Promise<File> {
60+
if ( this.fileUtil.isPublicFileType( file.type ) ) {
6261
file.external_file_id = file.type + '/' + file.reporter;
6362
} else {
6463
file.external_file_id = uuidv4();
6564
}
6665

6766
const buf = Buffer.from(
68-
file.data.replace(/^data:image\/\w+;base64,/, ''),
69-
'base64'
67+
file.data.replace( /^data:image\/\w+;base64,/, '' ),
68+
'base64'
7069
);
7170

7271
let res;
7372
try {
7473
res = await client
75-
.putObject({
76-
Body: buf,
77-
Key: file.external_file_id,
78-
Bucket: this.payload.bucketName,
79-
ContentType: file.content_type,
80-
ContentEncoding: 'base64',
81-
ACL: file.is_public ? 'public-read' : '',
82-
})
83-
.promise();
84-
} catch (err: any) {
85-
throw new Error(err.message);
74+
.putObject( {
75+
Body: buf,
76+
Key: file.external_file_id,
77+
Bucket: this.payload.bucketName,
78+
ContentType: file.content_type,
79+
ContentEncoding: 'base64',
80+
ACL: file.is_public ? 'public-read' : '',
81+
} )
82+
.promise();
83+
} catch ( err: any ) {
84+
throw new Error( err.message );
8685
}
8786

8887
file.uploaded = !res.$response.error;
@@ -98,19 +97,19 @@ export class S3FileService implements FileService {
9897
* @param client service client
9998
* @param externalFileId external file id
10099
*/
101-
async download(client: any, externalFileId: string): Promise<any> {
100+
async download( client: any, externalFileId: string ): Promise<any> {
102101
let res;
103102
try {
104103
res = await client
105-
.getObject({
106-
Key: externalFileId,
107-
Bucket: this.payload.bucketName,
108-
})
109-
.promise();
110-
} catch (err: any) {
111-
throw new Error(err.message);
104+
.getObject( {
105+
Key: externalFileId,
106+
Bucket: this.payload.bucketName,
107+
} )
108+
.promise();
109+
} catch ( err: any ) {
110+
throw new Error( err.message );
112111
}
113112

114-
return res.$response.data.Body.toString('base64');
113+
return res.$response.data.Body.toString( 'base64' );
115114
}
116115
}

app/util/file.util.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import { FileType } from '../enum/file-type.enum';
22

33
export class FileUtil {
4-
isPublicFileType = (type: FileType) => {
4+
isPublicFileType = ( type: FileType ) => {
55
if (
6-
type === FileType.TEAM_COVER_PICTURE ||
7-
type === FileType.TEAM_PROFILE_PICTURE ||
8-
type === FileType.USER_COVER_PICTURE ||
9-
type === FileType.USER_PROFILE_PICTURE
6+
type === FileType.TEAM_COVER_PICTURE ||
7+
type === FileType.TEAM_PROFILE_PICTURE ||
8+
type === FileType.USER_COVER_PICTURE ||
9+
type === FileType.USER_PROFILE_PICTURE
1010
) {
1111
return true;
1212
}

0 commit comments

Comments
 (0)