-
Notifications
You must be signed in to change notification settings - Fork 35
/
stou.rs
50 lines (47 loc) · 1.78 KB
/
stou.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//! The RFC 959 Store File Uniquely (`STOU`) command
use crate::server::chancomms::DataChanCmd;
use crate::{
auth::UserDetail,
server::controlchan::{
error::ControlChanError,
handler::{CommandContext, CommandHandler},
Reply, ReplyCode,
},
storage::{Metadata, StorageBackend},
};
use async_trait::async_trait;
use std::path::Path;
use uuid::Uuid;
// TODO: Write functional test for STOU command.
#[derive(Debug)]
pub struct Stou;
#[async_trait]
impl<Storager, User> CommandHandler<Storager, User> for Stou
where
User: UserDetail + 'static,
Storager: StorageBackend<User> + 'static,
Storager::Metadata: Metadata,
{
#[tracing_attributes::instrument]
async fn handle(&self, args: CommandContext<Storager, User>) -> Result<Reply, ControlChanError> {
let mut session = args.session.lock().await;
let uuid: String = Uuid::new_v4().to_string();
let filename: &Path = std::path::Path::new(&uuid);
let path: String = session.cwd.join(filename).to_string_lossy().to_string();
let logger = args.logger;
match session.data_cmd_tx.take() {
Some(tx) => {
tokio::spawn(async move {
if let Err(err) = tx.send(DataChanCmd::Stor { path }).await {
slog::warn!(logger, "STOU: could not send Stor command over data channel. {}", err);
}
});
Ok(Reply::new_with_string(ReplyCode::FileStatusOkay, filename.to_string_lossy().to_string()))
}
None => {
slog::warn!(logger, "STOU: no data connection established for STOU file {:?}", path);
Ok(Reply::new(ReplyCode::CantOpenDataConnection, "No data connection established"))
}
}
}
}