# HG changeset patch # User Sebastien Jodogne # Date 1602596208 -7200 # Node ID 38b24aa98ed3b0cab896f6831a0908f5870b0cca # Parent 112951171852e09f34e215a796fbb0ac4ad50e0f new lua sample: ClassifyUsingAet.lua diff -r 112951171852 -r 38b24aa98ed3 OrthancServer/Resources/Samples/Lua/ClassifyUsingAet.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/OrthancServer/Resources/Samples/Lua/ClassifyUsingAet.lua Tue Oct 13 15:36:48 2020 +0200 @@ -0,0 +1,49 @@ +-- Write the received DICOM instances to an external directory, and +-- group the patients by the source AET that has sent the DICOM +-- files. This sample is similar to "WriteToDisk.lua". + + +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 GetFromTable(t, key, defaultValue) + local result = t[key] + if result == nil or result == '' then + return defaultValue + else + return result + end +end + + +function OnStoredInstance(instanceId, tags, metadata, origin) + local dicom = RestApiGet('/instances/' .. instanceId .. '/file') + local tags = ParseJson(RestApiGet('/instances/' .. instanceId .. '/tags?simplify')) + local metadata = ParseJson(RestApiGet('/instances/' .. instanceId .. '/metadata?expand')) + + local path = ToAscii( + TARGET .. '/' .. + GetFromTable(metadata, 'RemoteAET', 'None') .. '/'.. + GetFromTable(tags, 'PatientID', '') .. ' - ' .. GetFromTable(tags, 'PatientName', '') .. '/' .. + GetFromTable(tags, 'StudyDate', '') .. ' - ' .. GetFromTable(tags, 'StudyDescription', '') .. '/' .. + GetFromTable(tags, 'SeriesDescription', '')) + + -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!) + -- http://stackoverflow.com/a/16029744/881731 + os.execute('mkdir -p "' .. path .. '"') + + -- Write to the file + local target = assert(io.open(path .. '/' .. GetFromTable(tags, 'SOPInstanceUID', 'none') .. '.dcm', 'wb')) + target:write(dicom) + target:close() + + -- Optional step: Remove the source file + RestApiDelete('/instances/' .. instanceId) +end