From f22bea78ff8292db270d1f1514347b97b2214483 Mon Sep 17 00:00:00 2001 From: Joseph Montanaro Date: Tue, 18 Jan 2022 22:10:06 -0800 Subject: [PATCH] use i64 for timestamp --- src/data.rs | 9 ++++++--- src/lib.rs | 13 ++++++++++++- src/schema.rs | 2 +- 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/data.rs b/src/data.rs index 1c6d02c..2b90c43 100644 --- a/src/data.rs +++ b/src/data.rs @@ -1,8 +1,9 @@ use rocket_sync_db_pools::database; -use rocket::serde::Serialize; +use serde::Serialize; use diesel::prelude::*; use crate::schema::uploads; +use crate::lib; #[database("punt")] @@ -13,8 +14,10 @@ pub struct DbConn(diesel::SqliteConnection); #[table_name="uploads"] pub struct FileInfo { pub id: String, + #[serde(default)] pub name: Option, - pub created_at: Option // figure out how to marshal this to a proper time type later + #[serde(default)] + pub created_at: Option // figure out how to marshal this to a proper time type later } @@ -27,7 +30,7 @@ pub async fn put_file_info(id: &str, name: Option<&str>, conn: DbConn) -> QueryR let file_info = FileInfo { id: id.to_owned(), name: name.map(|n| n.to_owned()), - created_at: Some(1642371865), + created_at: Some(lib::timestamp()), }; conn.run(|c| { diff --git a/src/lib.rs b/src/lib.rs index 3010215..a705b65 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,8 @@ +use std::io::ErrorKind; +use std::time::SystemTime; + use rand::{Rng, distributions::Alphanumeric}; use tokio::fs::create_dir; -use std::io::ErrorKind; use rocket::fs::TempFile; @@ -13,6 +15,15 @@ pub fn gen_id() -> String { } +pub fn timestamp() -> i64 { + let now = SystemTime::now(); + match now.duration_since(SystemTime::UNIX_EPOCH) { + Ok(dur) => dur.as_secs() as i64, + Err(e) => -(e.duration().as_secs() as i64), + } +} + + pub async fn persist_file(file: &mut TempFile<'_>, filename: &str) -> std::io::Result<()> { let dst = format!("uploads/{filename}"); match file.persist_to(&dst).await { diff --git a/src/schema.rs b/src/schema.rs index 9953dfc..93fc55a 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -2,6 +2,6 @@ table! { uploads (id) { id -> Text, name -> Nullable, - created_at -> Nullable, + created_at -> Nullable, } }