fluentci_graphql/schema/objects/
file.rs

1use std::sync::{Arc, Mutex};
2
3use crate::schema::objects::directory::Directory;
4use async_graphql::{Context, Error, Object, ID};
5use fluentci_common::{common, file};
6use fluentci_core::deps::Graph;
7use fluentci_types::file as types;
8
9#[derive(Debug, Clone, Default)]
10pub struct File {
11    pub id: ID,
12    pub path: String,
13}
14
15#[Object]
16impl File {
17    async fn id(&self) -> &ID {
18        &self.id
19    }
20
21    async fn path(&self) -> &str {
22        &self.path
23    }
24
25    async fn zip(&self, ctx: &Context<'_>) -> Result<File, Error> {
26        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
27        let file = common::zip(graph.clone(), self.path.clone())?;
28        Ok(File::from(file))
29    }
30
31    async fn tar_czvf(&self, ctx: &Context<'_>) -> Result<File, Error> {
32        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
33        let file = common::tar_czvf(graph.clone(), self.path.clone())?;
34        Ok(File::from(file))
35    }
36
37    async fn unzip(
38        &self,
39        ctx: &Context<'_>,
40        output_dir: Option<String>,
41    ) -> Result<Directory, Error> {
42        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
43        let dir = file::unzip(graph.clone(), self.path.clone(), output_dir)?;
44        Ok(Directory::from(dir))
45    }
46
47    async fn tar_xzvf(
48        &self,
49        ctx: &Context<'_>,
50        output_dir: Option<String>,
51    ) -> Result<Directory, Error> {
52        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
53        let dir = file::tar_xzvf(graph.clone(), self.path.clone(), output_dir)?;
54        Ok(Directory::from(dir))
55    }
56
57    async fn md5(&self, ctx: &Context<'_>) -> Result<String, Error> {
58        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
59        let hash = file::md5(graph.clone(), self.path.clone())?;
60        Ok(hash)
61    }
62
63    async fn sha256(&self, ctx: &Context<'_>) -> Result<String, Error> {
64        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
65        let hash = file::sha256(graph.clone(), self.path.clone())?;
66        Ok(hash)
67    }
68
69    async fn chmod(&self, ctx: &Context<'_>, mode: String) -> Result<File, Error> {
70        let graph = ctx.data::<Arc<Mutex<Graph>>>().unwrap();
71        let file = file::chmod(graph.clone(), self.path.clone(), mode)?;
72        Ok(File::from(file))
73    }
74}
75
76impl From<types::File> for File {
77    fn from(file: types::File) -> Self {
78        Self {
79            id: ID(file.id),
80            path: file.path,
81        }
82    }
83}