WriteToDisk.lua script

Dear Orthanc community,
I’m trying to customize the script for my needs. I need to zip the folder after writing to disk and delete the folder.

TARGET = ‘/tmp/lua’

function ToAscii(s)
http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
https://groups.google.com/d/msg/orthanc-users/qMLgkEmwwPI/6jRpCrlgBwAJ
return s:gsub(‘[^a-zA-Z0-9-/-:]’, ‘_’)
end

function OnStableSeries(seriesId, tags, metadata)
local instances = ParseJson(RestApiGet(‘/series/’ … seriesId)) [‘Instances’]
local patient = ParseJson(RestApiGet(‘/series/’ … seriesId … ‘/patient’)) [‘MainDicomTags’]
local study = ParseJson(RestApiGet(‘/series/’ … seriesId … ‘/study’)) [‘ID’]
local series = ParseJson(RestApiGet(‘/series/’ … seriesId)) [‘MainDicomTags’]

print('This series is now stable, writing its instances on the disk: ’ … ToAscii(patient[‘PatientName’] … ‘-’ … seriesId))

for i, instance in pairs(instances) do
local path = ToAscii(TARGET … ‘/’ … patient[‘PatientName’] … ‘/IMAGES’)

– Retrieve the DICOM file from Orthanc
local dicom = RestApiGet(‘/instances/’ … instance … ‘/file’)

– Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
http://stackoverflow.com/a/16029744/881731
os.execute(‘mkdir -p "’ … path … ‘"’)
ptname = ToAscii(patient[‘PatientName’])

– Write to the file
local target = assert(io.open(path … ‘/’ … instance … ‘.dcm’, ‘wb’))
target:write(dicom)
target:close()
RestApiDelete(‘/instances/’ … instance)
end
os.execute('/tmp/lua/zip.sh ’ … ToAscii(patient[‘PatientName’]))
end

os.execute(‘rm -rf’ … ToAscii(TARGET … ‘/’ … ToAscii(patient[‘PatientName’])))

How can i use variable patient after last “end”? It returns a nil value

I also tried (‘rm -rf’ … TARGET … ‘/’ … ptname))
ptname also returns a nil value

четверг, 27 мая 2021 г. в 11:01:16 UTC+3, Andrew:

Also had that doubt, gave up after a while

As far as I’m concerned, your script runs correctly on my computer.

Pay attention to the fact that the call to “mkdir” only correctly runs on GNU/Linux (as indicated in the comments).

You might be trying to handle a DICOM file that has no “PatientName” tag. You should modify the sample script to deal with such a situation.

Sébastien-

Thank you for reply.
Patients name exists. If I move rm command inside the function - it works. But if there two or more instances - rm command runs two or more times. :frowning:
If I move rm command at the end of script - Error “Can’t concatenate nil value”

Чт, 27 мая 2021 г. в 17:17, Sébastien Jodogne <s.jodogne@gmail.com>:

You obviously have to put the last “os.execute()” inside the function, otherwise variable “patient” is not defined (because “patient” is a local variable of the function).

I thought this should work:
Set global var ptname inside the function

ptname = ToAscii(patient[‘PatientName’])

And then os.execute outside the function

os.execute('rm -rf ’ … ToAscii(TARGET … ‘/’ … ptname))

But it doesn’t work :frowning:
Again ptname returns nil value

Чт, 27 мая 2021 г. в 17:17, Sébastien Jodogne <s.jodogne@gmail.com>:

The “os.remove()” outside the function is only executed once, when Orthanc starts. If you want it to be executed for each received series (which is visibly what you expect), it must be located within the callback function “OnStableSeries”.

I actually need to run rm once after receiving all series.

Чт, 27 мая 2021 г. в 17:43, Sébastien Jodogne <s.jodogne@gmail.com>:

Instead of discussing your code, you’ll find attached a complete working solution.

Note that using “os.execute()” is inherently insecure, and should be avoided in any production setup.

fixed.lua (1.07 KB)