-
-
Notifications
You must be signed in to change notification settings - Fork 245
Expand file tree
/
Copy pathupload.rs
More file actions
269 lines (252 loc) · 9.31 KB
/
upload.rs
File metadata and controls
269 lines (252 loc) · 9.31 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
#![expect(clippy::unwrap_used, reason = "contains legacy code which uses unwrap")]
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs;
use std::io::Read as _;
use std::path::Path;
use std::sync::Arc;
use std::time::Duration;
use anyhow::{bail, format_err, Result};
use clap::{Arg, ArgAction, ArgMatches, Command};
use log::warn;
use symbolic::debuginfo::sourcebundle::SourceFileType;
use crate::api::Api;
use crate::config::Config;
use crate::constants::DEFAULT_MAX_WAIT;
use crate::utils::args::validate_distribution;
use crate::utils::file_search::ReleaseFileSearch;
use crate::utils::file_upload::{
initialize_legacy_release_upload, FileUpload, SourceFile, UploadContext,
};
use crate::utils::fs::{decompress_gzip_content, is_gzip_compressed, path_as_url};
use crate::utils::progress::ProgressBarMode;
pub fn make_command(command: Command) -> Command {
command
.about("[DEPRECATED] Upload files for a release.")
.hide(true)
// Backward compatibility with `releases files <VERSION>` commands.
.arg(Arg::new("version").long("version").hide(true))
.arg(
Arg::new("path")
.value_name("PATH")
.required(true)
.help("The path to the file or directory to upload."),
)
.arg(
Arg::new("name")
.value_name("NAME")
.help("The name of the file on the server."),
)
.arg(
Arg::new("dist")
.long("dist")
.short('d')
.value_name("DISTRIBUTION")
.value_parser(validate_distribution)
.help("Optional distribution identifier for this file."),
)
.arg(
Arg::new("decompress")
.long("decompress")
.action(ArgAction::SetTrue)
.help("Enable files gzip decompression prior to upload."),
)
.arg(
Arg::new("wait")
.long("wait")
.action(ArgAction::SetTrue)
.conflicts_with("wait_for")
.help("Wait for the server to fully process uploaded files."),
)
.arg(
Arg::new("wait_for")
.long("wait-for")
.value_name("SECS")
.value_parser(clap::value_parser!(u64))
.conflicts_with("wait")
.help(
"Wait for the server to fully process uploaded files, \
but at most for the given number of seconds.",
),
)
.arg(
Arg::new("file-headers")
.long("file-header")
.short('H')
.value_name("KEY VALUE")
.action(ArgAction::Append)
.help("Store a header with this file."),
)
.arg(
Arg::new("url_prefix")
.short('u')
.long("url-prefix")
.value_name("PREFIX")
.help("The URL prefix to prepend to all filenames."),
)
.arg(
Arg::new("url_suffix")
.long("url-suffix")
.value_name("SUFFIX")
.help("The URL suffix to append to all filenames."),
)
.arg(
Arg::new("ignore")
.long("ignore")
.short('i')
.value_name("IGNORE")
.action(ArgAction::Append)
.help("Ignores all files and folders matching the given glob"),
)
.arg(
Arg::new("ignore_file")
.long("ignore-file")
.short('I')
.value_name("IGNORE_FILE")
.help(
"Ignore all files and folders specified in the given \
ignore file, e.g. .gitignore.",
),
)
.arg(
Arg::new("extensions")
.long("ext")
.short('x')
.value_name("EXT")
.action(ArgAction::Append)
.help(
"Set the file extensions that are considered for upload. \
This overrides the default extensions. To add an extension, all default \
extensions must be repeated. Specify once per extension.",
),
)
}
pub fn execute(matches: &ArgMatches) -> Result<()> {
let config = Config::current();
let release = config.get_release_with_legacy_fallback(matches)?;
let org = config.get_org(matches)?;
let project = config.get_project(matches).ok();
let api = Api::current();
let authenticated_api = api.authenticated()?;
let chunk_upload_options = authenticated_api.get_chunk_upload_options(&org)?;
let dist = matches.get_one::<String>("dist").map(String::as_str);
let mut headers = BTreeMap::new();
if let Some(header_list) = matches.get_many::<String>("file-headers") {
for header in header_list {
if !header.contains(':') {
bail!("Invalid header. Needs to be in key:value format");
}
let (key, value) = header.split_once(':').unwrap();
headers.insert(key.trim().to_owned(), value.trim().to_owned());
}
};
let wait_for_secs = matches.get_one::<u64>("wait_for").copied();
let wait = matches.get_flag("wait") || wait_for_secs.is_some();
let max_wait = wait_for_secs.map_or(DEFAULT_MAX_WAIT, Duration::from_secs);
let context = &UploadContext {
org: &org,
projects: &project.into_iter().collect::<Vec<_>>(),
release: Some(&release),
dist,
note: None,
wait,
max_wait,
dedupe: false,
chunk_upload_options: chunk_upload_options.as_ref(),
};
let path = Path::new(matches.get_one::<String>("path").unwrap());
// Batch files upload
if path.is_dir() {
let ignore_file = matches
.get_one::<String>("ignore_file")
.map(String::as_str)
.unwrap_or_default();
let ignores: Vec<_> = matches
.get_many::<String>("ignore")
.map(|ignores| ignores.map(|i| format!("!{i}")).collect())
.unwrap_or_default();
let extensions: Vec<_> = matches
.get_many::<String>("extensions")
.map(|extensions| extensions.map(|ext| ext.trim_start_matches('.')).collect())
.unwrap_or_default();
let sources = ReleaseFileSearch::new(path.to_path_buf())
.ignore_file(ignore_file)
.ignores(ignores)
.extensions(extensions)
.decompress(matches.get_flag("decompress"))
.collect_files()?;
let url_suffix = matches
.get_one::<String>("url_suffix")
.map(String::as_str)
.unwrap_or_default();
let mut url_prefix = matches
.get_one::<String>("url_prefix")
.map(String::as_str)
.unwrap_or("~");
// remove a single slash from the end. so ~/ becomes ~ and app:/// becomes app://
if url_prefix.ends_with('/') {
url_prefix = &url_prefix[..url_prefix.len() - 1];
}
let files = sources
.iter()
.map(|source| {
let local_path = source.path.strip_prefix(&source.base_path).unwrap();
let url = format!("{url_prefix}/{}{url_suffix}", path_as_url(local_path));
(
url.clone(),
SourceFile {
url,
path: source.path.clone(),
contents: Arc::new(source.contents.clone()),
ty: SourceFileType::Source,
headers: headers.clone(),
messages: vec![],
already_uploaded: false,
},
)
})
.collect();
FileUpload::new(context).files(&files).upload()
}
// Single file upload
else {
initialize_legacy_release_upload(context)?;
let name = match matches.get_one::<String>("name") {
Some(name) => name,
None => Path::new(path)
.file_name()
.and_then(OsStr::to_str)
.ok_or_else(|| format_err!("No filename provided."))?,
};
let mut f = fs::File::open(path)?;
let mut contents = Vec::new();
f.read_to_end(&mut contents)?;
if matches.get_flag("decompress") && is_gzip_compressed(&contents) {
contents = decompress_gzip_content(&contents).unwrap_or_else(|_| {
warn!("Could not decompress: {name}");
contents
});
}
if let Some(artifact) = authenticated_api
.region_specific(context.org)
.upload_release_file(
&context.try_into()?,
&contents,
name,
Some(
headers
.iter()
.map(|(k, v)| (k.clone(), v.clone()))
.collect::<Vec<_>>()
.as_slice(),
),
ProgressBarMode::Request,
)?
{
println!("A {} ({} bytes)", artifact.sha1, artifact.size);
} else {
bail!("File already present!");
}
Ok(())
}
}