Skip to content

Commit

Permalink
update checksum field and method of creating checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
slugger7 committed Jul 15, 2024
1 parent e321ea9 commit 8d3a3e3
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 85 deletions.
134 changes: 67 additions & 67 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion data/migrations/2024-07-01-143539_create_video/up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ create table video (
width int not null,
runtime bigint not null,
size bigint not null,
checksum char(32),
checksum char(40),
added timestamp default current_timestamp not null,
deleted boolean default false not null,
created timestamp default current_timestamp not null,
Expand Down
2 changes: 1 addition & 1 deletion data/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ diesel::table! {
width -> Int4,
runtime -> Int8,
size -> Int8,
#[max_length = 32]
#[max_length = 40]
checksum -> Nullable<Bpchar>,
added -> Timestamp,
deleted -> Bool,
Expand Down
2 changes: 1 addition & 1 deletion media/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
[dependencies]
ffmpeg-sidecar = "1.1.0"
hex = "0.4.3"
openssl = "0.10.64"
sha1 = "0.10.6"
24 changes: 9 additions & 15 deletions media/src/extensions.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::{fs::File, io::Read};

use openssl::sha;
use sha1::{Digest, Sha1};
use std::{fs::File, io};

pub fn create_relative_path(base_path: String, absolute_path: String) -> Result<String, String> {
if !absolute_path.contains(&base_path) {
Expand Down Expand Up @@ -31,20 +30,15 @@ pub fn file_size(path: &str) -> u64 {

pub fn checksum(path: &str) -> String {
println!("Hashing: {}", path);
if let Ok(f) = File::open(path) {
let mut hasher = sha::Sha1::new();
let bytes = f.bytes();
for byte in bytes {
match byte {
Ok(b) => hasher.update(&[b]),
Err(err) => eprintln!("checksum error: {}", err),
}
}
if let Ok(mut f) = File::open(path) {
let mut hasher = Sha1::new();

let hash = hasher.finish();
if let Err(err) = io::copy(&mut f, &mut hasher) {
eprintln!("Error copying file to hasher: {}", err);
}

println!("Hashing complete: {}", path);
return hex::encode(hash);
let hash_bytes = hasher.finalize();
return hex::encode(hash_bytes);
}

eprintln!("Something went wrong when hashing {}", path);
Expand Down

0 comments on commit 8d3a3e3

Please sign in to comment.