@@ -12,70 +12,74 @@ import { FileUtil } from '../util/file.util';
12
12
import { FileServiceWrapper } from '../wrapper/file-service.wrapper' ;
13
13
14
14
export class FileController {
15
- constructor ( private fileUtil : FileUtil = new FileUtil ( ) ) { }
15
+ constructor ( private fileUtil : FileUtil = new FileUtil ( ) ) {
16
+ }
17
+
16
18
/**
17
19
* creates file
18
20
* @param context context
19
21
* @param file file
20
22
*/
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 ) )
23
25
throw new Error (
24
- 'File must contain contentType, title, description and data'
26
+ 'File must contain contentType, title, description and data'
25
27
) ;
26
28
27
29
const serviceClient = await this . getServiceClient (
28
- context . mongodb_provider ,
29
- context . serviceKey
30
+ context . mongodb_provider ,
31
+ context . serviceKey
30
32
) ;
31
33
32
34
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 )
36
38
) {
37
39
file . reporter = context . username ;
38
40
}
39
41
40
- file = await serviceClient . service . upload ( serviceClient . client , file ) ;
42
+ file = await serviceClient . service . upload ( serviceClient . client , file ) ;
41
43
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 ) ;
44
46
return fileRepository . saveFile (
45
- file . reporter || context . username ,
46
- file ,
47
- context . serviceKey
47
+ file . reporter || context . username ,
48
+ file ,
49
+ context . serviceKey
48
50
) ;
49
51
}
50
52
51
- throw new Error ( 'File upload failed' ) ;
53
+ if ( ! this . fileUtil . isPublicFileType ( file . type ) ) {
54
+ throw new Error ( 'File upload failed' ) ;
55
+ }
52
56
} ;
53
57
54
58
/**
55
59
* gets file by id
56
60
* @param context context
57
61
* @param id file id
58
62
*/
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 ) ;
61
65
62
- const file = await fileRepository . getFile ( context . username , id ) ;
66
+ const file = await fileRepository . getFile ( context . username , id ) ;
63
67
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 ) ;
66
70
}
67
71
68
72
const serviceClient = await this . getServiceClient (
69
- context . mongodb_provider ,
70
- file . service_key
73
+ context . mongodb_provider ,
74
+ file . service_key
71
75
) ;
72
76
73
- if ( file . is_public ) {
77
+ if ( file . is_public ) {
74
78
file . url = serviceClient . publicUrl + file . external_file_id ;
75
79
} else {
76
80
file . data = await serviceClient . service . download (
77
- serviceClient . client ,
78
- file . external_file_id
81
+ serviceClient . client ,
82
+ file . external_file_id
79
83
) ;
80
84
}
81
85
@@ -88,17 +92,17 @@ export class FileController {
88
92
* @param serviceKey service key
89
93
*/
90
94
private getServiceClient = async (
91
- provider : MongoDbProvider ,
92
- serviceKey : string
95
+ provider : MongoDbProvider ,
96
+ serviceKey : string
93
97
) : Promise < ServiceClient > => {
94
- const serviceConfig = await this . getServiceConfig ( provider , serviceKey ) ;
98
+ const serviceConfig = await this . getServiceConfig ( provider , serviceKey ) ;
95
99
96
- const service = new FileServiceWrapper ( serviceConfig . payload . service ) ;
100
+ const service = new FileServiceWrapper ( serviceConfig . payload . service ) ;
97
101
98
- const client = await service . initializeClient ( serviceConfig ) ;
102
+ const client = await service . initializeClient ( serviceConfig ) ;
99
103
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' ) ;
102
106
103
107
return {
104
108
client,
@@ -113,19 +117,19 @@ export class FileController {
113
117
* @param serviceKey service key
114
118
*/
115
119
private getServiceConfig = async (
116
- provider : MongoDbProvider ,
117
- serviceKey : string
120
+ provider : MongoDbProvider ,
121
+ serviceKey : string
118
122
) : Promise < any > => {
119
123
const conn = provider . getConnection ( ) ;
120
124
121
125
const serviceProviderRepository =
122
- await new ServiceProviderRepository ( ) . initialize ( conn ) ;
126
+ await new ServiceProviderRepository ( ) . initialize ( conn ) ;
123
127
124
128
let serviceConfig : any =
125
- await serviceProviderRepository . getServiceProviderByKey ( serviceKey ) ;
129
+ await serviceProviderRepository . getServiceProviderByKey ( serviceKey ) ;
126
130
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' ) ;
129
133
130
134
return serviceConfig ;
131
135
} ;
@@ -134,13 +138,13 @@ export class FileController {
134
138
* checks is file valid
135
139
* @param file file
136
140
*/
137
- private isValidFile = ( file : File ) : boolean => {
141
+ private isValidFile = ( file : File ) : boolean => {
138
142
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
144
148
) {
145
149
return true ;
146
150
}
0 commit comments