Stebalien / tempfile

Temporary file library for rust
http://stebalien.com/projects/tempfile-rs
Apache License 2.0
1.2k stars 120 forks source link

Persists Function results in an error #245

Closed slayernominee closed 1 year ago

slayernominee commented 1 year ago
error[E0277]: the trait bound `PersistError: ResponseError` is not satisfied
   --> src/api.rs:195:66
    |
195 |         let mut persisted_file = file.persist("./saved_file.txt")?;
    |                                                                  ^ the trait `ResponseError` is not implemented for `PersistError`
    |
    = help: the following other types implement trait `ResponseError`:
              BlockingError
              Box<(dyn StdError + 'static)>
              CorsError
              HttpError
              Infallible
              InvalidHeaderValue
              JsonFieldError
              JsonPayloadError
            and 22 others
    = note: required for `actix_web::Error` to implement `std::convert::From<PersistError>`
    = note: required for `Result<_, actix_web::Error>` to implement `FromResidual<Result<Infallible, PersistError>>`

when using the code example from the documentation:

use tempfile::NamedTempFile;

let file = NamedTempFile::new()?;

let mut persisted_file = file.persist("./saved_file.txt")?;
writeln!(persisted_file, "Brian was here. Briefly.")?;

this is my whole method

#[post("/uploadImage")]
async fn upload_image(
    MultipartForm(form): MultipartForm<UploadForm>,
) -> Result<impl Responder, Error> {

        let file = NamedTempFile::new()?;

        let mut persisted_file = file.persist("./saved_file.txt")?;
        writeln!(persisted_file, "Brian was here. Briefly.")?;

        //f.file.persist(path).unwrap();

    Ok(HttpResponse::Ok())
}
slayernominee commented 1 year ago

f.file.persist(path).unwrap(); results without the other code in this error:

thread 'actix-rt|system:0|arbiter:0' panicked at 'called `Result::unwrap()` on an `Err` value: PersistError(Os { code: 18, kind: CrossesDevices, message: "Invalid cross-device link" })', src/api.rs:190:30

that also shouldnt result in any error from the examples from the actix project

Stebalien commented 1 year ago

I'd take a closer look at that error message in your first post. You need to convert the error from this library into the appropriate error for your application.

slayernominee commented 1 year ago

the final error is this one: PersistError(Os { code: 18, kind: CrossesDevices, message: "Invalid cross-device link" }) but why do i get it? im using one partition ... no docker nothing

Stebalien commented 1 year ago

I assume you're writing the temporary file in /tmp? That's likely in memory.

slayernominee commented 1 year ago

nope in the current working directy (in a subfolder of the home directory)

Stebalien commented 1 year ago

NamedTempFile::new() will create a temporary file in your system's temporary file directory. You need NamedTempFile::new_in().

slayernominee commented 1 year ago

results in the same error if i specify my home directory

slayernominee commented 1 year ago

but only on my manjaro system (using a btfrs formated partition), on a mac it just works fine the same code

Stebalien commented 1 year ago

Unfortunately, this is an OS error so there's not much I can do but help debug. At this point, I'd recommend printing out the .path() of the resulting temporary file and your current working directory to see if they appear to be on different filesystems (compare with the output of findmnt).