cognite/dto/core/files/
file.rs

1use serde::{Deserialize, Serialize};
2use serde_with::skip_serializing_none;
3use std::collections::HashMap;
4
5use crate::{
6    models::instances::InstanceId, CogniteExternalId, EqIdentity, Identity, IdentityOrInstance,
7    IntoPatch, IntoPatchItem, Patch, UpdateList, UpdateMap, UpdateSetNull, UpsertOptions,
8};
9
10#[skip_serializing_none]
11#[derive(Serialize, Deserialize, Debug, Default, Clone)]
12#[serde(rename_all = "camelCase")]
13/// Description of a CDF file.
14pub struct FileMetadata {
15    /// File external ID. Must be unique accross all files in the project.
16    pub external_id: Option<String>,
17    /// File name.
18    pub name: String,
19    /// Directory containing the file. Must be an absolute, unix-style path.
20    pub directory: Option<String>,
21    /// Source of the file.
22    pub source: Option<String>,
23    /// File mime type, e.g. `application/pdf`.
24    pub mime_type: Option<String>,
25    /// Custom, application specific metadata. String key -> String value.
26    /// Limits: Maximum length of key is 128 bytes, value 10240 bytes,
27    /// up to 256 key-value pairs, of total size at most 10240.
28    pub metadata: Option<HashMap<String, String>>,
29    /// List of assets the file is tied to.
30    pub asset_ids: Option<Vec<i64>>,
31    /// Data set the file belongs to.
32    pub data_set_id: Option<i64>,
33    /// Timestamp in milliseconds since epoch when this file was created in the source system.
34    pub source_created_time: Option<i64>,
35    /// Timestamp in milliseconds since epoch when this file was last modified in the source system.
36    pub source_modified_time: Option<i64>,
37    /// The required security categories to access this file.
38    pub security_categories: Option<Vec<i64>>,
39    /// List of labels associated with this file.
40    pub labels: Option<Vec<CogniteExternalId>>,
41    /// File internal ID.
42    pub id: i64,
43    /// Whether or not the actual file is uploaded.
44    pub uploaded: bool,
45    /// Time this file was uploaded, in milliseconds since epoch.
46    pub uploaded_time: Option<i64>,
47    /// Time this file was created, in milliseconds since epoch.
48    pub created_time: i64,
49    /// Time this file was last modified, in milliseconds since epoch.
50    pub last_updated_time: i64,
51}
52
53#[derive(Serialize, Deserialize, Debug, Default, Clone)]
54#[serde(rename_all = "camelCase")]
55/// Extra data in file upload result from normal file upload.
56pub struct UploadUrl {
57    /// URL for uploading data to this file. Returned only in response to
58    /// `upload`.
59    pub upload_url: String,
60    /// Optional instance id of the file if in data models.
61    pub instance_id: Option<InstanceId>,
62}
63
64#[derive(Serialize, Deserialize, Debug, Default, Clone)]
65#[serde(rename_all = "camelCase")]
66/// Extra data in file upload result from multipart file upload.
67pub struct MultiUploadUrls {
68    /// Identifier for this multipart upload, to be used in `complete_multipart_upload`.
69    pub upload_id: String,
70    /// Upload URL for each part of the file.
71    pub upload_urls: Vec<String>,
72    /// Optional instance id of the file if in data models.
73    pub instance_id: Option<InstanceId>,
74}
75
76#[derive(Serialize, Deserialize, Debug, Default, Clone)]
77#[serde(rename_all = "camelCase")]
78/// Result for uploading a file object to CDF.
79pub struct FileUploadResult<T> {
80    #[serde(flatten)]
81    /// File metadata object.
82    pub metadata: FileMetadata,
83    #[serde(flatten)]
84    /// Any extra fields, specific fields depend on endpoint.
85    pub extra: T,
86}
87
88#[skip_serializing_none]
89#[derive(Serialize, Deserialize, Debug, Default, Clone)]
90#[serde(rename_all = "camelCase")]
91/// Create a new file.
92pub struct AddFile {
93    /// File external ID. Must be unique accross all files in the project.
94    pub external_id: Option<String>,
95    /// File name.
96    pub name: String,
97    /// Directory containing the file. Must be an absolute, unix-style path.
98    pub directory: Option<String>,
99    /// Source of the file.
100    pub source: Option<String>,
101    /// File mime type, e.g. `application/pdf`.
102    pub mime_type: Option<String>,
103    /// Custom, application specific metadata. String key -> String value.
104    /// Limits: Maximum length of key is 128 bytes, value 10240 bytes,
105    /// up to 256 key-value pairs, of total size at most 10240.
106    pub metadata: Option<HashMap<String, String>>,
107    /// List of assets the file is tied to.
108    pub asset_ids: Option<Vec<i64>>,
109    /// Data set the file belongs to.
110    pub data_set_id: Option<i64>,
111    /// Timestamp in milliseconds since epoch when this file was created in the source system.
112    pub source_created_time: Option<i64>,
113    /// Timestamp in milliseconds since epoch when this file was last modified in the source system.
114    pub source_modified_time: Option<i64>,
115    /// The required security categories to access this file.
116    pub security_categories: Option<Vec<i64>>,
117    /// List of labels associated with this file.
118    pub labels: Option<Vec<CogniteExternalId>>,
119}
120
121impl From<FileMetadata> for AddFile {
122    fn from(file: FileMetadata) -> AddFile {
123        AddFile {
124            external_id: file.external_id,
125            name: file.name,
126            directory: file.directory,
127            source: file.source,
128            mime_type: file.mime_type,
129            metadata: file.metadata,
130            asset_ids: file.asset_ids,
131            data_set_id: file.data_set_id,
132            source_created_time: file.source_created_time,
133            source_modified_time: file.source_modified_time,
134            security_categories: file.security_categories,
135            labels: file.labels,
136        }
137    }
138}
139
140impl EqIdentity for AddFile {
141    fn eq(&self, id: &Identity) -> bool {
142        match id {
143            Identity::Id { id: _ } => false,
144            Identity::ExternalId { external_id } => self.external_id.as_ref() == Some(external_id),
145        }
146    }
147}
148
149#[skip_serializing_none]
150#[derive(Serialize, Deserialize, Debug, Default, Clone)]
151#[serde(rename_all = "camelCase")]
152/// Update a file.
153pub struct PatchFile {
154    /// File external ID. Must be unique accross all files in the project.
155    pub external_id: Option<UpdateSetNull<String>>,
156    /// Directory containing the file. Must be an absolute, unix-style path.
157    pub directory: Option<UpdateSetNull<String>>,
158    /// Source of the file.
159    pub source: Option<UpdateSetNull<String>>,
160    /// File mime type, e.g. `application/pdf`.
161    pub mime_type: Option<UpdateSetNull<String>>,
162    /// Custom, application specific metadata. String key -> String value.
163    /// Limits: Maximum length of key is 128 bytes, value 10240 bytes,
164    /// up to 256 key-value pairs, of total size at most 10240.
165    pub metadata: Option<UpdateMap<String, String>>,
166    /// List of assets the file is tied to.
167    pub asset_ids: Option<UpdateList<i64, i64>>,
168    /// Timestamp in milliseconds since epoch when this file was created in the source system.
169    pub source_created_time: Option<UpdateSetNull<i64>>,
170    /// Timestamp in milliseconds since epoch when this file was last modified in the source system.
171    pub source_modified_time: Option<UpdateSetNull<i64>>,
172    /// Data set the file belongs to.
173    pub data_set_id: Option<UpdateSetNull<i64>>,
174    /// The required security categories to access this file.
175    pub security_categories: Option<UpdateList<i64, i64>>,
176    /// List of labels associated with this file.
177    pub labels: Option<UpdateList<CogniteExternalId, CogniteExternalId>>,
178}
179
180impl IntoPatch<Patch<PatchFile>> for FileMetadata {
181    fn patch(self, options: &UpsertOptions) -> Patch<PatchFile> {
182        Patch::<PatchFile> {
183            id: to_idt!(self),
184            update: PatchFile {
185                external_id: self.external_id.patch(options),
186                directory: self.directory.patch(options),
187                source: self.source.patch(options),
188                mime_type: self.mime_type.patch(options),
189                metadata: self.metadata.patch(options),
190                asset_ids: self.asset_ids.patch(options),
191                source_created_time: self.source_created_time.patch(options),
192                source_modified_time: self.source_modified_time.patch(options),
193                data_set_id: self.data_set_id.patch(options),
194                security_categories: self.security_categories.patch(options),
195                labels: self.labels.patch(options),
196            },
197        }
198    }
199}
200
201impl IntoPatch<PatchFile> for AddFile {
202    fn patch(self, options: &UpsertOptions) -> PatchFile {
203        PatchFile {
204            external_id: self.external_id.patch(options),
205            directory: self.directory.patch(options),
206            source: self.source.patch(options),
207            mime_type: self.mime_type.patch(options),
208            metadata: self.metadata.patch(options),
209            asset_ids: self.asset_ids.patch(options),
210            source_created_time: self.source_created_time.patch(options),
211            source_modified_time: self.source_modified_time.patch(options),
212            data_set_id: self.data_set_id.patch(options),
213            security_categories: self.security_categories.patch(options),
214            labels: self.labels.patch(options),
215        }
216    }
217}
218
219impl From<FileMetadata> for Patch<PatchFile> {
220    fn from(file: FileMetadata) -> Self {
221        IntoPatch::<Patch<PatchFile>>::patch(file, &Default::default())
222    }
223}
224
225#[derive(Serialize, Deserialize, Debug)]
226#[serde(rename_all = "camelCase")]
227/// Download URL for a file
228pub struct FileDownloadUrl {
229    #[serde(flatten)]
230    /// ID of the file.
231    pub id: IdentityOrInstance,
232    /// Temporary download URL for the file.
233    pub download_url: String,
234}
235
236#[derive(Serialize, Deserialize, Debug)]
237#[serde(rename_all = "camelCase")]
238/// Aggregates on files.
239pub struct FileAggregates {
240    /// Number of files in the project.
241    pub count: i64,
242}
243
244#[derive(Serialize, Deserialize, Debug)]
245#[serde(rename_all = "camelCase")]
246/// Payload for the `complete_multipart_upload` endpoint.
247pub struct CompleteMultipartUpload {
248    #[serde(flatten)]
249    /// ID of the file.
250    pub id: IdentityOrInstance,
251    /// Upload ID returned by `init_multipart_upload`
252    pub upload_id: String,
253}