shadow_drive_sdk/client/delete_file.rs
1use serde_json::{json, Value};
2use solana_sdk::{pubkey::Pubkey, signer::Signer};
3
4use super::ShadowDriveClient;
5use crate::{constants::SHDW_DRIVE_ENDPOINT, error::Error, models::*};
6
7impl<T> ShadowDriveClient<T>
8where
9 T: Signer,
10{
11 /// Marks a file for deletion from the Shadow Drive.
12 /// Files marked for deletion are deleted at the end of each Solana epoch.
13 /// Marking a file for deletion can be undone with `cancel_delete_file`,
14 /// but this must be done before the end of the Solana epoch.
15 /// * `storage_account_key` - The public key of the [`StorageAccount`](crate::models::StorageAccount) that contains the file.
16 /// * `url` - The Shadow Drive url of the file you want to mark for deletion.
17 /// # Example
18 ///
19 /// ```
20 /// # use shadow_drive_rust::{ShadowDriveClient, derived_addresses::storage_account};
21 /// # use solana_client::rpc_client::RpcClient;
22 /// # use solana_sdk::{
23 /// # pubkey::Pubkey,
24 /// # signature::Keypair,
25 /// # signer::{keypair::read_keypair_file, Signer},
26 /// # };
27 /// #
28 /// # let keypair = read_keypair_file(KEYPAIR_PATH).expect("failed to load keypair at path");
29 /// # let user_pubkey = keypair.pubkey();
30 /// # let rpc_client = RpcClient::new("https://ssc-dao.genesysgo.net");
31 /// # let shdw_drive_client = ShadowDriveClient::new(keypair, rpc_client);
32 /// # let (storage_account_key, _) = storage_account(&user_pubkey, 0);
33 /// # let url = String::from("https://shdw-drive.genesysgo.net/B7Qk2omAvchkePhdGovCVQuVpZHcieqPQCwFxeeBZGuT/file.txt");
34 /// #
35 /// let delete_file_response = shdw_drive_client
36 /// .delete_file(&storage_account_key, url)
37 /// .await?;
38 /// ```
39 pub async fn delete_file(
40 &self,
41 storage_account_key: &Pubkey,
42 url: String,
43 ) -> ShadowDriveResult<DeleteFileResponse> {
44 let message_to_sign = delete_file_message(storage_account_key, &url);
45
46 //Signature implements Display as a base58 encoded string
47 let signature = self
48 .wallet
49 .sign_message(message_to_sign.as_bytes())
50 .to_string();
51
52 let body = json!({
53 "signer": self.wallet.pubkey(),
54 "message": signature,
55 "location": url,
56 });
57
58 let response = self
59 .http_client
60 .post(format!("{}/delete-file", SHDW_DRIVE_ENDPOINT))
61 .json(&body)
62 .send()
63 .await?;
64
65 if !response.status().is_success() {
66 return Err(Error::ShadowDriveServerError {
67 status: response.status().as_u16(),
68 message: response.json::<Value>().await?,
69 });
70 }
71
72 let response = response.json::<DeleteFileResponse>().await?;
73
74 Ok(response)
75 }
76}
77
78fn delete_file_message(storage_account_key: &Pubkey, url: &str) -> String {
79 format!(
80 "Shadow Drive Signed Message:\nStorageAccount: {}\nFile to delete: {}",
81 storage_account_key, url
82 )
83}