haskelldevilrepa

Write to file, but overwrite it if it exists with DevIL and REPA


I am using the DevIL library to read and write images. The problems is that I want to overwrite the file if it already exists.

Here is my code:

(RGB v) <- runIL $ readImage "/foo/foo.png"
let rotated = (computeS $ batman v) :: Array F DIM3 Word8
runIL $ writeImage ("/foo/foo.png") (RGB rotated)

How can I achieve that? Can I do this or do I have to think of another way? It is something of the OS?

I am using that file as a temporary image until the user decides to save it (after some changes, like rotating it, expanding it, etc).


Solution

  • If the library won't let you replace an existing file directly, you can check whether the file exists with doesFileExist and delete it before you save the new file.

    replace = do    
      let fn = "path/to/image/file/..."
      exists <- doesFileExist fn
      when exists $ removeFile fn
      runIL $ writeImage fn (RGB rotated)