LTN12 pump issues

I’ve had a problem that has been giving me quite a bit of trouble the past few days.

I have a network request that is reaching out to a mySQL database to retrieve images. These images come back with base64 encoding and are concatenated into one large string i.e. userPhoto1[imageBase64]userPhoto2[imageBase64]…etc.

The goal of this is to take that string, chop it up using a while loop and ltn12, and pump each image string to its own image file.

The problem is that when I try to loop through ltn12.pump.all(source, sink) I always get an error on the 2nd file:

Runtime error

attempt to use a closed file

stack traceback:

C: in function ‘close’

?: in function ‘?’

?: in function <?:255>

?: in function <?:273>

?: in function ‘all’

<path to my lua project with the line that the 2nd ltn12.pump.all() is called>

Has anyone out there ever tried to loop through pumps? Is there a better/easier way to do this?

I get no problem if I loop through with ltn12.pump.step(source, sink) except that my files are incomplete because the steps aren’t large enough. This leads me to believe that the error isn’t with my loop since it’s able to pass through the master image file.

I’ve also tried simplifying my code to have it ltn12.pump.all() the same image 3 times and I get the same error.

Any help would be greatly appreciated.

The below example is just me trying to pump the same image 3 times. If I do it once, it works without issue.

[lua] outFileArray = system.pathForFile(“userPhotosout” …“0”…".png", system.DocumentsDirectory)
openCode = io.open(outFileArray,“wb”)
sinkFile = ltn12.sink.file(openCode)

rturn,error = ltn12.pump.all(
ltn12.source.string(string.sub(event.response,11,2550)),
ltn12.sink.chain(decode, sinkFile)
)
outFileArray2 = system.pathForFile(“userPhotosout” …“1”…".png", system.DocumentsDirectory)
openCode2 = io.open(outFileArray2,“wb”)
sinkFile2 = ltn12.sink.file(openCode2)

rturn2,error2 = ltn12.pump.all(
ltn12.source.string(string.sub(event.response,11,2550)),
ltn12.sink.chain(decode, sinkFile2)
)

outFileArray3 = system.pathForFile(“userPhotosout” …“2”…".png", system.DocumentsDirectory)
openCode3 = io.open(outFileArray3,“wb”)
sinkFile3 = ltn12.sink.file(openCode3)

rturn3, error3 = ltn12.pump.all(
ltn12.source.string(string.sub(event.response,11,2550)),
ltn12.sink.chain(decode, sinkFile3)

)[/lua]