Skip to content

Commit

Permalink
Writing changed to copying. (#44)
Browse files Browse the repository at this point in the history
Signed-off-by: Pavel Kirilin <[email protected]>
  • Loading branch information
s3rius committed Feb 24, 2022
1 parent 0ccead6 commit 6029124
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -113,5 +113,5 @@ httptest = "0.15.4"
[profile.release]
lto = true
panic = "abort"
opt-level = "z"
opt-level = 2
codegen-units = 1
30 changes: 7 additions & 23 deletions src/info_storages/file_info_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;
use async_trait::async_trait;
use log::error;
use tokio::fs::{read_to_string, remove_file, DirBuilder, OpenOptions};
use tokio::io::{AsyncWriteExt, BufWriter};
use tokio::io::copy;

use crate::errors::{RustusError, RustusResult};
use crate::info_storages::{FileInfo, InfoStorage};
Expand Down Expand Up @@ -35,7 +35,7 @@ impl InfoStorage for FileInfoStorage {
}

async fn set_info(&self, file_info: &FileInfo, create: bool) -> RustusResult<()> {
let file = OpenOptions::new()
let mut file = OpenOptions::new()
.write(true)
.create(create)
.truncate(true)
Expand All @@ -45,27 +45,11 @@ impl InfoStorage for FileInfoStorage {
error!("{:?}", err);
RustusError::UnableToWrite(err.to_string())
})?;
let mut writer = BufWriter::new(file);
writer
.write(
serde_json::to_string(&file_info)
.map_err(|err| {
error!("{:#?}", err);
err
})?
.as_bytes(),
)
.await
.map_err(|err| {
error!("{:?}", err);
RustusError::UnableToWrite(
self.info_file_path(file_info.id.as_str())
.as_path()
.display()
.to_string(),
)
})?;
writer.flush().await?;
let data = serde_json::to_string(&file_info).map_err(|err| {
error!("{:#?}", err);
err
})?;
copy(&mut data.as_bytes(), &mut file).await?;
Ok(())
}

Expand Down
1 change: 1 addition & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![cfg_attr(coverage, feature(no_coverage))]

use std::str::FromStr;
use std::sync::Arc;

Expand Down
15 changes: 4 additions & 11 deletions src/storages/file_storage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use actix_files::NamedFile;
use async_trait::async_trait;
use log::error;
use tokio::fs::{remove_file, DirBuilder, OpenOptions};
use tokio::io::{copy, AsyncWriteExt, BufWriter};
use tokio::io::{copy, BufReader};

use crate::errors::{RustusError, RustusResult};
use crate::info_storages::FileInfo;
Expand Down Expand Up @@ -85,7 +85,7 @@ impl Storage for FileStorage {
// Opening file in w+a mode.
// It means that we're going to append some
// bytes to the end of a file.
let file = OpenOptions::new()
let mut file = OpenOptions::new()
.write(true)
.append(true)
.create(false)
Expand All @@ -95,15 +95,8 @@ impl Storage for FileStorage {
error!("{:?}", err);
RustusError::UnableToWrite(err.to_string())
})?;
let mut writer = BufWriter::new(file);
writer.write(bytes).await.map_err(|err| {
error!("{:?}", err);
RustusError::UnableToWrite(info.path.clone().unwrap())
})?;
writer.flush().await.map_err(|err| {
error!("{:?}", err);
RustusError::UnableToWrite(info.path.clone().unwrap())
})?;
let mut reader = BufReader::new(bytes);
copy(&mut reader, &mut file).await?;
Ok(())
}

Expand Down

0 comments on commit 6029124

Please sign in to comment.