Skip to content

Commit

Permalink
release: v0.4.0, implemented file directory tree.
Browse files Browse the repository at this point in the history
  • Loading branch information
zensh committed Jul 9, 2024
1 parent 8a7d16e commit e6c9da5
Show file tree
Hide file tree
Showing 5 changed files with 150 additions and 94 deletions.
53 changes: 46 additions & 7 deletions src/ic_oss_bucket/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,54 @@ dfx deploy ic_oss_bucket --argument "(opt variant {Init =
enable_hash_index = true;
}
})"
# Output:
# ...
# Installing code for canister ic_oss_bucket, with canister ID mmrxu-fqaaa-aaaap-ahhna-cai
# Deployed canisters.
# URLs:
# Backend canister via Candid interface:
# ic_oss_bucket: http://127.0.0.1:4943/?canisterId=bd3sg-teaaa-aaaaa-qaaba-cai&id=mmrxu-fqaaa-aaaap-ahhna-cai

dfx canister call ic_oss_bucket get_bucket_info '(null)'

MYID=$(dfx identity get-principal)
ic-oss-cli -i debug/uploader.pem identity
# principal: nprym-ylvyz-ig3fr-lgcmn-zzzt4-tyuix-3v6bm-fsel7-6lq6x-zh2w7-zqe

dfx canister call ic_oss_bucket admin_set_managers "(vec {principal \"$MYID\"; principal \"nprym-ylvyz-ig3fr-lgcmn-zzzt4-tyuix-3v6bm-fsel7-6lq6x-zh2w7-zqe\"})"

dfx canister call ic_oss_bucket list_files '(0, null, null, null)'
dfx canister call ic_oss_bucket list_folders '(0, null)'

ic-oss-cli -i debug/uploader.pem upload -b mmrxu-fqaaa-aaaap-ahhna-cai --file README.md

dfx canister call ic_oss_bucket get_file_info '(1, null)'

dfx canister call ic_oss_bucket update_file_info "(record {
id = 1;
status = opt 0;
}, null)"

dfx canister call ic_oss_bucket create_folder "(record {
parent = 0;
name = \"home\";
}, null)"
dfx canister call ic_oss_bucket list_folders '(0, null)'

dfx canister call ic_oss_bucket create_folder "(record {
parent = 1;
name = \"jarvis\";
}, null)"

dfx canister call ic_oss_bucket move_file "(record {
id = 1;
from = 0;
to = 2;
}, null)"

dfx canister call ic_oss_bucket list_files '(2, null, null, null)'

dfx canister call ic_oss_bucket delete_file '(1, null)'
```

Download the file in browser:
- by file id: `http://mmrxu-fqaaa-aaaap-ahhna-cai.localhost:4943/f/1`
- by file hash: `http://mmrxu-fqaaa-aaaap-ahhna-cai.localhost:4943/h/b7bb9040d64479a7ca56c8e03ae2daddc819859f7b858488c0b998eeded6dede`


## License
Copyright © 2024 [LDC Labs](https://github.com/ldclabs).

Expand Down
10 changes: 9 additions & 1 deletion src/ic_oss_bucket/src/api_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -176,9 +176,17 @@ fn http_request(request: HttpRequest) -> HttpStreamingResponse {
metadata.content_type.clone()
};

let filename = if param.inline {
""
} else if let Some(ref name) = param.name {
name
} else {
&metadata.name
};

headers.push((
"content-disposition".to_string(),
content_disposition(&metadata.name),
content_disposition(filename),
));

// return all chunks for small file
Expand Down
47 changes: 13 additions & 34 deletions src/ic_oss_bucket/src/api_update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ fn create_file(
store::fs::update_chunk(id, i as u32, now_ms, chunk.to_vec())?;
}

if let Some(status) = input.status {
store::fs::update_file(id, |metadata| {
metadata.status = status;
})?;
if input.status.is_some() {
store::fs::update_file(
UpdateFileInput {
id,
status: input.status,
..Default::default()
},
now_ms,
)?;
}
}

Expand Down Expand Up @@ -98,27 +103,9 @@ fn update_file_info(
Ok(())
})?;

let now_ms = ic_cdk::api::time() / MILLISECONDS;
store::fs::update_file(input.id, |metadata| {
if let Some(name) = input.name {
metadata.name = name;
}
if let Some(content_type) = input.content_type {
metadata.content_type = content_type;
}
if let Some(status) = input.status {
metadata.status = status;
}
if input.hash.is_some() {
metadata.hash = input.hash;
}
if input.custom.is_some() {
metadata.custom = input.custom;
}
metadata.updated_at = now_ms;
})?;

Ok(UpdateFileOutput { updated_at: now_ms })
let updated_at = ic_cdk::api::time() / MILLISECONDS;
store::fs::update_file(input, updated_at)?;
Ok(UpdateFileOutput { updated_at })
}

#[ic_cdk::update(guard = "is_controller_or_manager")]
Expand Down Expand Up @@ -207,15 +194,7 @@ fn update_folder_info(
input.validate()?;

let updated_at = ic_cdk::api::time() / MILLISECONDS;
store::fs::update_folder(input.id, |metadata| {
if let Some(name) = input.name {
metadata.name = name;
}
if let Some(status) = input.status {
metadata.status = status;
}
metadata.updated_at = updated_at;
})?;
store::fs::update_folder(input, updated_at)?;

Ok(UpdateFolderOutput { updated_at })
}
Expand Down
Loading

0 comments on commit e6c9da5

Please sign in to comment.