-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmod.rs
More file actions
394 lines (369 loc) · 13.4 KB
/
mod.rs
File metadata and controls
394 lines (369 loc) · 13.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
use serde::Deserialize;
use serde::Serialize;
use strum::Display;
use strum::EnumString;
use uuid::Uuid;
use crate::model::Timestamp;
#[cfg(feature = "backend")]
use crate::schema::file;
#[cfg(feature = "backend")]
use crate::schema::file_upload;
#[cfg(feature = "backend")]
use thoth_errors::{ThothError, ThothResult};
#[cfg_attr(
feature = "backend",
derive(diesel_derive_enum::DbEnum, juniper::GraphQLEnum),
graphql(description = "Type of file being uploaded"),
ExistingTypePath = "crate::schema::sql_types::FileType"
)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, EnumString, Display)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[strum(serialize_all = "lowercase")]
pub enum FileType {
#[cfg_attr(
feature = "backend",
db_rename = "publication",
graphql(description = "Publication file (PDF, EPUB, XML, etc.)")
)]
Publication,
#[cfg_attr(
feature = "backend",
db_rename = "frontcover",
graphql(description = "Front cover image")
)]
Frontcover,
#[cfg_attr(
feature = "backend",
db_rename = "additional_resource",
graphql(description = "Additional resource file (audio, video, image, spreadsheet, etc.)")
)]
#[strum(serialize = "additional_resource")]
AdditionalResource,
#[cfg_attr(
feature = "backend",
db_rename = "work_featured_video",
graphql(description = "Featured video file hosted on CDN")
)]
#[strum(serialize = "work_featured_video")]
WorkFeaturedVideo,
#[cfg_attr(
feature = "backend",
db_rename = "accessibility_report",
graphql(description = "Accessibility report")
)]
#[strum(serialize = "accessibility_report")]
A11yReport,
}
#[cfg_attr(
feature = "backend",
derive(diesel_derive_enum::DbEnum, juniper::GraphQLEnum),
graphql(description = "Algorithm used to create file checksum"),
ExistingTypePath = "crate::schema::sql_types::ChecksumAlgorithm"
)]
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, EnumString, Display)]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum ChecksumAlgorithm {
#[cfg_attr(feature = "backend", db_rename = "MD5")]
Md5,
#[cfg_attr(feature = "backend", db_rename = "SHA256")]
Sha256,
#[cfg_attr(feature = "backend", db_rename = "SHA1")]
Sha1,
}
#[cfg_attr(feature = "backend", derive(diesel::Queryable))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct File {
pub file_id: Uuid,
pub file_type: FileType,
pub work_id: Option<Uuid>,
pub publication_id: Option<Uuid>,
pub additional_resource_id: Option<Uuid>,
pub work_featured_video_id: Option<Uuid>,
pub object_key: String,
pub cdn_url: String,
pub mime_type: String,
pub bytes: i64,
pub sha256: String,
pub created_at: Timestamp,
pub updated_at: Timestamp,
}
#[cfg(feature = "backend")]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct FileCleanupCandidate {
pub file_type: FileType,
pub object_key: String,
}
#[cfg_attr(feature = "backend", derive(diesel::Queryable))]
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "camelCase")]
pub struct FileUpload {
pub file_upload_id: Uuid,
pub file_type: FileType,
pub work_id: Option<Uuid>,
pub publication_id: Option<Uuid>,
pub additional_resource_id: Option<Uuid>,
pub work_featured_video_id: Option<Uuid>,
pub declared_mime_type: String,
pub declared_extension: String,
pub declared_sha256: String,
pub created_at: Timestamp,
pub updated_at: Timestamp,
}
#[cfg_attr(
feature = "backend",
derive(juniper::GraphQLInputObject, diesel::Insertable),
graphql(description = "Input for starting a publication file upload"),
diesel(table_name = file_upload)
)]
pub struct NewFileUpload {
pub file_type: FileType,
pub work_id: Option<Uuid>,
pub publication_id: Option<Uuid>,
pub additional_resource_id: Option<Uuid>,
pub work_featured_video_id: Option<Uuid>,
pub declared_mime_type: String,
pub declared_extension: String,
pub declared_sha256: String,
}
#[cfg_attr(
feature = "backend",
derive(diesel::Insertable),
diesel(table_name = file)
)]
pub struct NewFile {
pub file_type: FileType,
pub work_id: Option<Uuid>,
pub publication_id: Option<Uuid>,
pub additional_resource_id: Option<Uuid>,
pub work_featured_video_id: Option<Uuid>,
pub object_key: String,
pub cdn_url: String,
pub mime_type: String,
pub bytes: i64,
pub sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(description = "Input for starting a publication file upload (PDF, EPUB, XML, etc.).")]
pub struct NewPublicationFileUpload {
#[graphql(description = "Thoth ID of the publication linked to this file.")]
pub publication_id: Uuid,
#[graphql(
description = "MIME type declared by the client (used for validation and in the presigned URL)."
)]
pub declared_mime_type: String,
#[graphql(
description = "File extension to use in the final canonical key, e.g. 'pdf', 'epub', 'xml'."
)]
pub declared_extension: String,
#[graphql(description = "SHA-256 checksum of the file, hex-encoded.")]
pub declared_sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(description = "Input for starting a front cover upload for a work.")]
pub struct NewFrontcoverFileUpload {
#[graphql(description = "Thoth ID of the work this front cover belongs to.")]
pub work_id: Uuid,
#[graphql(description = "MIME type declared by the client (e.g. 'image/jpeg').")]
pub declared_mime_type: String,
#[graphql(
description = "File extension to use in the final canonical key, e.g. 'jpg', 'png', 'webp'."
)]
pub declared_extension: String,
#[graphql(description = "SHA-256 checksum of the file, hex-encoded.")]
pub declared_sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(description = "Input for starting an upload for an additional resource asset.")]
pub struct NewAdditionalResourceFileUpload {
#[graphql(description = "Thoth ID of the additional resource linked to this file.")]
pub additional_resource_id: Uuid,
#[graphql(
description = "MIME type declared by the client (used for validation and in the presigned URL)."
)]
pub declared_mime_type: String,
#[graphql(
description = "File extension to use in the final canonical key, e.g. 'jpg', 'png', 'mp4', 'xlsx'."
)]
pub declared_extension: String,
#[graphql(description = "SHA-256 checksum of the file, hex-encoded.")]
pub declared_sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(description = "Input for starting an upload for a work featured video.")]
pub struct NewWorkFeaturedVideoFileUpload {
#[graphql(description = "Thoth ID of the work featured video linked to this file.")]
pub work_featured_video_id: Uuid,
#[graphql(
description = "MIME type declared by the client (used for validation and in the presigned URL)."
)]
pub declared_mime_type: String,
#[graphql(
description = "File extension to use in the final canonical key, e.g. 'mp4', 'webm', 'mov'."
)]
pub declared_extension: String,
#[graphql(description = "SHA-256 checksum of the file, hex-encoded.")]
pub declared_sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(description = "Input for starting an upload for an accessibility report.")]
pub struct NewA11yReportFileUpload {
#[graphql(description = "Thoth ID of the publication linked to this file.")]
pub publication_id: Uuid,
#[graphql(
description = "MIME type declared by the client (used for validation and in the presigned URL)."
)]
pub declared_mime_type: String,
#[graphql(
description = "File extension to use in the final canonical key, e.g. 'html', 'pdf'."
)]
pub declared_extension: String,
#[graphql(description = "SHA-256 checksum of the file, hex-encoded.")]
pub declared_sha256: String,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLInputObject)]
#[graphql(
description = "Input for completing a file upload and promoting it to its final DOI-based location."
)]
pub struct CompleteFileUpload {
#[graphql(description = "ID of the upload session to complete.")]
pub file_upload_id: Uuid,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLObject)]
#[graphql(
description = "Response from initiating a file upload, containing the upload URL and expiration time."
)]
pub struct FileUploadResponse {
#[graphql(description = "ID of the upload session.")]
pub file_upload_id: Uuid,
#[graphql(description = "Presigned S3 PUT URL for uploading the file.")]
pub upload_url: String,
#[graphql(description = "Headers that must be sent with the HTTP PUT request to uploadUrl.")]
pub upload_headers: Vec<UploadRequestHeader>,
#[graphql(description = "Time when the upload URL expires.")]
pub expires_at: Timestamp,
}
#[cfg(feature = "backend")]
#[derive(juniper::GraphQLObject)]
#[graphql(description = "Single required HTTP header for presigned file upload.")]
pub struct UploadRequestHeader {
#[graphql(description = "HTTP header name.")]
pub name: String,
#[graphql(description = "HTTP header value.")]
pub value: String,
}
#[cfg(feature = "backend")]
pub fn upload_request_headers(
declared_mime_type: &str,
declared_sha256: &str,
) -> ThothResult<Vec<UploadRequestHeader>> {
use base64::{engine::general_purpose, Engine as _};
let sha256_bytes = hex::decode(declared_sha256)
.map_err(|e| ThothError::InternalError(format!("Invalid SHA-256 hex: {}", e)))?;
let sha256_base64 = general_purpose::STANDARD.encode(sha256_bytes);
Ok(vec![
UploadRequestHeader {
name: "Content-Type".to_string(),
value: declared_mime_type.to_string(),
},
UploadRequestHeader {
name: "x-amz-checksum-sha256".to_string(),
value: sha256_base64,
},
UploadRequestHeader {
name: "x-amz-sdk-checksum-algorithm".to_string(),
value: "SHA256".to_string(),
},
])
}
#[cfg(feature = "backend")]
impl From<NewPublicationFileUpload> for NewFileUpload {
fn from(data: NewPublicationFileUpload) -> Self {
NewFileUpload {
file_type: FileType::Publication,
work_id: None,
publication_id: Some(data.publication_id),
additional_resource_id: None,
work_featured_video_id: None,
declared_mime_type: data.declared_mime_type,
declared_extension: data.declared_extension.to_lowercase(),
declared_sha256: data.declared_sha256,
}
}
}
#[cfg(feature = "backend")]
impl From<NewFrontcoverFileUpload> for NewFileUpload {
fn from(data: NewFrontcoverFileUpload) -> Self {
NewFileUpload {
file_type: FileType::Frontcover,
work_id: Some(data.work_id),
publication_id: None,
additional_resource_id: None,
work_featured_video_id: None,
declared_mime_type: data.declared_mime_type,
declared_extension: data.declared_extension.to_lowercase(),
declared_sha256: data.declared_sha256,
}
}
}
#[cfg(feature = "backend")]
impl From<NewAdditionalResourceFileUpload> for NewFileUpload {
fn from(data: NewAdditionalResourceFileUpload) -> Self {
NewFileUpload {
file_type: FileType::AdditionalResource,
work_id: None,
publication_id: None,
additional_resource_id: Some(data.additional_resource_id),
work_featured_video_id: None,
declared_mime_type: data.declared_mime_type,
declared_extension: data.declared_extension.to_lowercase(),
declared_sha256: data.declared_sha256,
}
}
}
#[cfg(feature = "backend")]
impl From<NewWorkFeaturedVideoFileUpload> for NewFileUpload {
fn from(data: NewWorkFeaturedVideoFileUpload) -> Self {
NewFileUpload {
file_type: FileType::WorkFeaturedVideo,
work_id: None,
publication_id: None,
additional_resource_id: None,
work_featured_video_id: Some(data.work_featured_video_id),
declared_mime_type: data.declared_mime_type,
declared_extension: data.declared_extension.to_lowercase(),
declared_sha256: data.declared_sha256,
}
}
}
#[cfg(feature = "backend")]
impl From<NewA11yReportFileUpload> for NewFileUpload {
fn from(data: NewA11yReportFileUpload) -> Self {
NewFileUpload {
file_type: FileType::A11yReport,
work_id: None,
publication_id: Some(data.publication_id),
additional_resource_id: None,
work_featured_video_id: None,
declared_mime_type: data.declared_mime_type,
declared_extension: data.declared_extension.to_lowercase(),
declared_sha256: data.declared_sha256,
}
}
}
#[cfg(feature = "backend")]
pub mod crud;
#[cfg(feature = "backend")]
mod policy;
#[cfg(feature = "backend")]
pub(crate) use policy::FilePolicy;
#[cfg(test)]
mod tests;