1use anyhow::anyhow;
4use serde::{Deserialize, Serialize};
5
6use crate::canvas::CanvasInformation;
7use crate::models::prelude::*;
8use crate::parameters::*;
9use crate::requests::*;
10
11#[derive(Debug, Deserialize, Serialize)]
12pub struct File {
13 pub id: usize,
14 pub uuid: Option<String>,
15 pub folder_id: Option<usize>,
16 pub display_name: Option<String>,
17 pub filename: Option<String>,
18 #[serde(rename(deserialize = "content-type"))]
19 pub content_type: Option<String>,
20 pub url: Option<String>,
21 pub size: Option<usize>,
22 pub created_at: Option<String>,
23 pub updated_at: Option<String>,
24 pub unlock_at: Option<String>,
25 pub locked: Option<bool>,
26 pub hidden: Option<bool>,
27 pub thumbnail_url: Option<String>,
28 pub modified_at: Option<String>,
29 pub mime_class: Option<String>,
30 pub media_entry_id: Option<String>,
31 pub locked_for_user: Option<bool>,
32 pub lock_info: Option<String>,
33 pub lock_explanation: Option<String>,
34 pub preview_url: Option<String>,
35}
36
37impl File {
38 #[cfg(not(feature = "blocking"))]
39 pub async fn download(&self, canvas: &CanvasInformation<'_>, path: &str) -> anyhow::Result<()> {
40 let url = self
41 .url
42 .clone()
43 .ok_or_else(|| anyhow!("File url not set"))?;
44 let name = self
45 .filename
46 .clone()
47 .ok_or_else(|| anyhow!("File name not set"))?;
48
49 let mut resp = canvas.get_request(url).send().await?.bytes().await?;
50
51 std::fs::write(format!("{path}/{name}"), resp)?;
52
53 Ok(())
54 }
55
56 #[cfg(feature = "blocking")]
57 pub fn download(&self, canvas: &CanvasInformation<'_>, path: &str) -> anyhow::Result<()> {
58 let url = self
59 .url
60 .clone()
61 .ok_or_else(|| anyhow!("File url not set"))?;
62 let name = self
63 .filename
64 .clone()
65 .ok_or_else(|| anyhow!("File name not set"))?;
66
67 let mut resp = canvas.get_request(url).send()?.bytes()?;
68
69 std::fs::write(format!("{path}/{name}"), resp)?;
70
71 Ok(())
72 }
73
74 api_todo! {
75 delete(self)
77 }
78
79 api_todo! {
80 get_contents(self)
82 }
83}