fn file_response(path_row: rusqlite::Result<String>) -> Result<Response, Rejection> {
    let raw_path = match path_row {
        Ok(path) => path,
        Err(rusqlite::Error::QueryReturnedNoRows) => {
            let response = json!({"status_code": StatusCode::NOT_FOUND.as_u16()});
            return Ok(warp::reply::json(&response).into_response());
        }
        Err(e) => {
            error!("Failed to query path from files table: {}", e);
            return Err(Error::DatabaseFailedInternally.into());
        }
    };

    // Try to read the file
    let path = Path::new(&raw_path);
    let mut file = match std::fs::File::open(path) {
        Ok(file) => file,
        Err(e) => {
            error!("Couldn't read file: {}.", e);
            return Err(Error::ValidationFailed.into());
        }
    };

    let mut bytes = vec![];
    if let Err(e) = file.read_to_end(&mut bytes) {
        error!("Couldn't read file: {}.", e);
        return Err(Error::DatabaseFailedInternally.into());
    }
    // Base64 encode the result
    let base64_encoded_bytes = base64::encode(bytes);
    // Return
    let json = GenericStringResponse {
        status_code: StatusCode::OK.as_u16(),
        result: base64_encoded_bytes,
    };
    Ok(warp::reply::json(&json).into_response())
}
// Takes a .query_row response returning the `path` field from a files row and builds it into a
// file response.
Takes a .query_row response returning the `path` field from a files row and builds it into a
file response.
Takes a . query_row response returning the ` path ` field from a files row and builds it into a file response .
fn file_response(path_row: rusqlite::Result<String>) -> Result<Response, Rejection> {
    let raw_path = match path_row {
        Ok(path) => path,
        Err(rusqlite::Error::QueryReturnedNoRows) => {
            let response = json!({"status_code": StatusCode::NOT_FOUND.as_u16()});
            return Ok(warp::reply::json(&response).into_response());
        }
        Err(e) => {
            error!("Failed to query path from files table: {}", e);
            return Err(Error::DatabaseFailedInternally.into());
        }
    };
    // Try to read the file
    let path = Path::new(&raw_path);
    let mut file = match std::fs::File::open(path) {
        Ok(file) => file,
        Err(e) => {
            error!("Couldn't read file: {}.", e);
            return Err(Error::ValidationFailed.into());
        }
    };
    let mut bytes = vec![];
    if let Err(e) = file.read_to_end(&mut bytes) {
        error!("Couldn't read file: {}.", e);
        return Err(Error::DatabaseFailedInternally.into());
    }
    // Base64 encode the result
    let base64_encoded_bytes = base64::encode(bytes);
    // Return
    let json = GenericStringResponse {
        status_code: StatusCode::OK.as_u16(),
        result: base64_encoded_bytes,
    };
    Ok(warp::reply::json(&json).into_response())
}
fn file_response ( path_row : rusqlite :: Result < String > ) -> Result < Response , Rejection > { let raw_path = match path_row { Ok ( path ) => path , Err ( rusqlite :: Error :: QueryReturnedNoRows ) => { let response = json ! ( { " " StatusCode NOT_FOUND as_u16 ( ) } ) ; return Ok ( warp :: reply :: json ( & response ) . into_response ( ) ) ; } Err ( e ) => { error ! ( " " e ) ; return Err ( Error :: DatabaseFailedInternally . into ( ) ) ; } } ; // Try to read the file let path = Path :: new ( & raw_path ) ; let mut file = match std :: fs :: File :: open ( path ) { Ok ( file ) => file , Err ( e ) => { error ! ( " " e ) ; return Err ( Error :: ValidationFailed . into ( ) ) ; } } ; let mut bytes = vec ! [ ] ; if let Err ( e ) = file . read_to_end ( & mut bytes ) { error ! ( " " e ) ; return Err ( Error :: DatabaseFailedInternally . into ( ) ) ; } // Base64 encode the result let base64_encoded_bytes = base64 :: encode ( bytes ) ; // Return let json = GenericStringResponse { status_code : StatusCode :: OK . as_u16 ( ) , result : base64_encoded_bytes , } ; Ok ( warp :: reply :: json ( & json ) . into_response ( ) ) }
Takes a .query_row response returning the `path` field from a files row and builds it into a
file response.
Takes a . query_row response returning the ` path ` field from a files row and builds it into a file response .
path_row
rusqlite::Result<String>
path_row
rusqlite::Result<String>