Skip to content

Commit d402a58

Browse files
metamejoepio
authored andcommitted
static str lovelies
1 parent 3c52750 commit d402a58

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

server/src/files.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,8 @@ pub enum FileStore {
1010
}
1111

1212
impl FileStore {
13-
pub fn get_file_store(file_id: &str) -> FileStore {
14-
if file_id.starts_with("s3:") {
15-
FileStore::S3
16-
} else {
17-
FileStore::FS
18-
}
19-
}
13+
const S3_PREFIX: &'static str = "s3:";
14+
const FS_PREFIX: &'static str = "fs:";
2015

2116
pub fn get_config_file_store(config: &Config) -> FileStore {
2217
if config.opts.s3_bucket.is_some() {
@@ -27,25 +22,29 @@ impl FileStore {
2722
}
2823

2924
pub fn get_subject_file_store(subject: &str) -> FileStore {
30-
if subject.contains("s3:") {
25+
if subject.contains(Self::S3_PREFIX) {
3126
FileStore::S3
3227
} else {
3328
FileStore::FS
3429
}
3530
}
3631

37-
pub fn name(&self) -> &str {
32+
pub fn prefix(&self) -> &str {
3833
match self {
39-
Self::S3 => "s3",
40-
Self::FS => "fs",
34+
Self::S3 => Self::S3_PREFIX,
35+
Self::FS => Self::FS_PREFIX,
4136
}
4237
}
4338

39+
pub fn encoded(&self) -> String {
40+
urlencoding::encode(self.prefix()).into_owned()
41+
}
42+
4443
}
4544

4645
impl fmt::Display for FileStore {
4746
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
48-
write!(f, "{}", self.name())
47+
write!(f, "{}", self.prefix())
4948
}
5049
}
5150

server/src/handlers/download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub async fn handle_download(
2929
let for_agent = get_client_agent(headers, &appstate, subject.clone())?;
3030
tracing::info!("handle_download: {}", subject);
3131
let file_store = FileStore::get_subject_file_store(&subject);
32-
let encoded = subject.replace(&format!("{file_store}:"), &format!("{file_store}%3A"));
32+
let encoded = subject.replace(file_store.prefix(), &file_store.encoded());
3333
let resource = store.get_resource_extended(&encoded, false, &for_agent)?;
3434
let file_id = resource
3535
.get(urls::INTERNAL_ID)

server/src/handlers/upload.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub async fn upload_handler(
8181
.map_err(|_e| "Too large")?;
8282

8383
let file_store = FileStore::get_config_file_store(&appstate.config);
84-
let file_id = format!("{}:{}", &file_store, &fs_file_id);
84+
let file_id = format!("{}{}", file_store.prefix(), &fs_file_id);
8585
if let FileStore::S3 = file_store {
8686
files::s3_upload_object(&appstate, &file_id, &file_path).await?;
8787
fs::remove_file(&file_path)?;

0 commit comments

Comments
 (0)