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
//! Upload input types and file replacement policies.
use std::fmt;
use std::io::Read;
use std::path::PathBuf;
use mime::Mime;
/// Policy for reconciling existing draft files with new uploads.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum FileReplacePolicy {
/// Delete all visible draft files before uploading.
ReplaceAll,
/// Replace files that share the same filename.
UpsertByFilename,
/// Keep existing files and add new uploads alongside them.
KeepExistingAndAdd,
}
/// Source data for a single upload.
pub enum UploadSource {
/// Upload from a local file path.
Path(
/// Local source path.
PathBuf,
),
/// Upload from a blocking reader with an explicit content length.
Reader {
/// Reader that produces the upload bytes.
reader: Box<dyn Read + Send>,
/// Exact number of bytes that the reader will produce.
content_length: u64,
},
}
impl fmt::Debug for UploadSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Path(path) => f.debug_tuple("Path").field(path).finish(),
Self::Reader { content_length, .. } => f
.debug_struct("Reader")
.field("content_length", content_length)
.finish_non_exhaustive(),
}
}
}
/// Specification for one file upload.
#[derive(Debug)]
pub struct UploadSpec {
/// Filename to expose in Zenodo.
pub filename: String,
/// Upload source.
pub source: UploadSource,
/// MIME type to send with the upload.
pub content_type: Mime,
}
impl UploadSpec {
/// Builds an upload spec from a local path.
///
/// Zenodo bucket uploads commonly expect `application/octet-stream`, so
/// that is the safe default used here. Callers can still override
/// [`Self::content_type`] explicitly before upload when needed.
///
/// # Examples
///
/// ```
/// use std::path::PathBuf;
/// use zenodo_rs::UploadSpec;
///
/// let spec = UploadSpec::from_path(PathBuf::from("/tmp/archive.tar.gz"))?;
/// assert_eq!(spec.filename, "archive.tar.gz");
/// # Ok::<(), std::io::Error>(())
/// ```
///
/// # Errors
///
/// Returns an error if the path does not contain a final filename segment.
pub fn from_path(path: impl Into<PathBuf>) -> std::io::Result<Self> {
let path = path.into();
let filename = path
.file_name()
.map(|name| name.to_string_lossy().into_owned())
.ok_or_else(path_without_filename_error)?;
Ok(Self {
content_type: mime::APPLICATION_OCTET_STREAM,
filename,
source: UploadSource::Path(path),
})
}
/// Builds an upload spec from a reader and explicit metadata.
///
/// # Examples
///
/// ```
/// use std::io::Cursor;
/// use zenodo_rs::{UploadSource, UploadSpec};
///
/// let spec = UploadSpec::from_reader(
/// "artifact.bin",
/// Cursor::new(vec![1_u8, 2, 3]),
/// 3,
/// mime::APPLICATION_OCTET_STREAM,
/// );
///
/// assert_eq!(spec.filename, "artifact.bin");
/// match spec.source {
/// UploadSource::Reader { content_length, .. } => assert_eq!(content_length, 3),
/// UploadSource::Path(_) => unreachable!("expected reader source"),
/// }
/// ```
#[must_use]
pub fn from_reader(
filename: impl Into<String>,
reader: impl Read + Send + 'static,
content_length: u64,
content_type: Mime,
) -> Self {
Self {
filename: filename.into(),
source: UploadSource::Reader {
reader: Box::new(reader),
content_length,
},
content_type,
}
}
}
fn path_without_filename_error() -> std::io::Error {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
"path has no final file name segment",
)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::{path_without_filename_error, UploadSource, UploadSpec};
#[test]
fn path_upload_defaults_to_octet_stream() {
let spec = UploadSpec::from_path(PathBuf::from("/tmp/archive.tar.gz")).unwrap();
assert_eq!(spec.filename, "archive.tar.gz");
assert_eq!(spec.content_type, mime::APPLICATION_OCTET_STREAM);
}
#[test]
fn path_upload_rejects_missing_filename() {
let error = UploadSpec::from_path(PathBuf::from("/")).unwrap_err();
assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
}
#[test]
fn missing_filename_error_has_stable_message() {
let error = path_without_filename_error();
assert_eq!(error.kind(), std::io::ErrorKind::InvalidInput);
assert_eq!(error.to_string(), "path has no final file name segment");
}
#[test]
fn reader_upload_debug_hides_reader() {
let spec = UploadSpec::from_reader(
"artifact.bin",
std::io::Cursor::new(vec![1, 2, 3]),
3,
mime::APPLICATION_OCTET_STREAM,
);
match spec.source {
UploadSource::Reader { content_length, .. } => assert_eq!(content_length, 3),
UploadSource::Path(_) => panic!("expected reader source"),
}
assert!(format!("{spec:?}").contains("artifact.bin"));
}
#[test]
fn path_source_debug_shows_path_variant() {
let spec = UploadSpec::from_path(PathBuf::from("/tmp/report.txt")).unwrap();
assert!(format!("{:?}", spec.source).contains("Path"));
match spec.source {
UploadSource::Path(path) => assert_eq!(path, PathBuf::from("/tmp/report.txt")),
UploadSource::Reader { .. } => panic!("expected path source"),
}
}
}