Fix incorrect ID handling

This commit is contained in:
Niels Andriesse 2021-04-27 11:42:00 +10:00
parent 416dedb193
commit de16e74d53
3 changed files with 9 additions and 9 deletions

View File

@ -158,7 +158,7 @@ pub async fn store_file(
// INSERT rather than REPLACE so that on the off chance there's already a file with this exact
// id (i.e. timestamp) we simply error out and get the client to retry.
let stmt = format!("INSERT INTO {} (id, timestamp) VALUES (?1, ?2)", storage::FILES_TABLE);
let _ = match conn.execute(&stmt, params![id as i64, now]) {
let _ = match conn.execute(&stmt, params![id.to_string(), now]) {
Ok(rows) => rows,
Err(e) => {
error!("Couldn't insert file record due to error: {}.", e);

View File

@ -126,7 +126,7 @@ fn create_room_tables_if_needed(conn: &DatabaseConnection) {
// Note that a given public key can have multiple pending tokens
let pending_tokens_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
public_key STRING,
public_key TEXT,
timestamp INTEGER,
token BLOB
)",
@ -138,7 +138,7 @@ fn create_room_tables_if_needed(conn: &DatabaseConnection) {
// The token is stored as hex here (rather than as bytes) because it's more convenient for lookup
let tokens_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
public_key STRING PRIMARY KEY,
public_key TEXT PRIMARY KEY,
timestamp INTEGER,
token TEXT
)",
@ -148,7 +148,7 @@ fn create_room_tables_if_needed(conn: &DatabaseConnection) {
// Files
let files_table_cmd = format!(
"CREATE TABLE IF NOT EXISTS {} (
id INTEGER PRIMARY KEY,
id TEXT PRIMARY KEY,
timestamp INTEGER
)",
FILES_TABLE
@ -262,10 +262,10 @@ pub async fn prune_files(file_expiration: i64) {
return error!("Couldn't prune files due to error: {}.", e);
}
};
let ids: Vec<i64> = rows.filter_map(|result| result.ok()).collect();
let ids: Vec<String> = rows.filter_map(|result| result.ok()).collect();
if !ids.is_empty() {
// Delete the files
let mut deleted_ids: Vec<i64> = vec![];
let mut deleted_ids: Vec<String> = vec![];
for id in ids {
match fs::remove_file(format!("files/{}", id)) {
Ok(_) => deleted_ids.push(id),

View File

@ -1,5 +1,4 @@
use std::collections::HashMap;
use std::convert::TryFrom;
use std::fs;
use std::path::Path;
@ -87,8 +86,9 @@ async fn test_file_handling() {
// Check that there's a file record
let conn = pool.get().unwrap();
let raw_query = format!("SELECT id FROM {}", storage::FILES_TABLE);
let _id: i64 = conn.query_row(&raw_query, params![], |row| Ok(row.get(0)?)).unwrap();
let id = u64::try_from(_id).unwrap();
let id_as_string: String =
conn.query_row(&raw_query, params![], |row| Ok(row.get(0)?)).unwrap();
let id = id_as_string.parse::<u64>().unwrap();
// Retrieve the file and check the content
let base64_encoded_file =
handlers::get_file(id, Some(auth_token.clone()), &pool).await.unwrap().result;