Create a temporary file from a base64 string with rapture-io
Asked Answered
C

1

6

So, basically I want to create a temporary file from a base64 string content. Right now, I'm doing this with native java-io functions. But I would like to achieve the same result using the rapture-io library for scala.

So my question would be, is it possible to achieve this with rapture-io, and if so, how?

I already went through the documentation, but is not specific enough:

https://github.com/propensive/rapture-io/blob/master/doc/introduction.md

Here's my actual code:

import org.apache.commons.codec.binary.Base64
import java.io.FileOutputStream
import java.io.File

val data: String = base64StringContent //Base64 String content of the file.
val fileName = myFileName
val fileExt = myFileExt

//It does write the file in my temp folder.
val file: File = File.createTempFile(fileName, fileExt)
val fileByteArray: Array[Byte] = Base64.decodeBase64(data)
val fileOutFile: FileOutputStream = new FileOutputStream(file)
fileOutFile.write(fileByteArray)
fileOutFile.close()
file.deleteOnExit()
file
Cocci answered 3/11, 2014 at 15:46 Comment(0)
D
1

Does this work for you?

import rapture.fs.platform.posix
import rapture.io._
import rapture.core._
import rapture.fs._
import strategy.throwExceptions

 val tmpFile  = (File / "tmp").tempFile(prefix = "yourfileName",suffix = ".extension")
 "data" >> tmpFile
 tmpFile.deleteOnExit()
 tmpFile.delete()

Untested on Windows. You might have to use a different separator e.g. \\ instead of /

import rapture.fs.platform.windows
import rapture.io._
import rapture.core._
import rapture.fs._
import strategy.throwExceptions

 val tmpFile  = (File / "C:" / "Windows" / "Temp" ).tempFile(prefix = "yourfileName",suffix = ".extension")
 "data" >> tmpFile
 tmpFile.deleteOnExit()
 tmpFile.delete()
Defroster answered 4/11, 2014 at 2:13 Comment(5)
Hi! Thanks for your answer! Code is breaking in "val tmpFile..." line, getting: "The system cannot find the path specified". I'm under windows env... Should I change that for any other folder/route? Any other alternative that could work for both envs? (I'm planning to run this in Heroku)Cocci
See edited answer for an untested solution. I imagine you would have to conditionally import the platform based on some other information. You could put the platform in a config file or base it off of querying System.properties. You could also conditionally create the fileUrl based on the platform type.Defroster
Windows route did work like that, just needed to add the ':' after the 'C'. But here's the thing... Is does create the file on my 'Temp' folder, but all of them are "corrupted". I'm assuming "data" is my base64 string value, right? ("data" >> tmpFile)Cocci
Why do you say it is corrupted? What are the contents and what do you expect? You might have to change the encoding with something like import encodings.UTF-8Defroster
My bad :p. Instead of "data" >> tmpFile, I changed it for: val fileByteArray: Array[Byte] = Base64.decodeBase64(data) fileByteArray >> tmpFile Working great! For any type of files: images, audio, etc Thank you so much! :)Cocci

© 2022 - 2024 — McMap. All rights reserved.