comparison OrthancServer/Resources/Samples/Lua/WriteToDisk.lua @ 4044:d25f4c0fa160 framework

splitting code into OrthancFramework and OrthancServer
author Sebastien Jodogne <s.jodogne@gmail.com>
date Wed, 10 Jun 2020 20:30:34 +0200
parents Resources/Samples/Lua/WriteToDisk.lua@4555a8ef2e88
children
comparison
equal deleted inserted replaced
4043:6c6239aec462 4044:d25f4c0fa160
1 TARGET = '/tmp/lua'
2
3 function ToAscii(s)
4 -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub
5 -- https://groups.google.com/d/msg/orthanc-users/qMLgkEmwwPI/6jRpCrlgBwAJ
6 return s:gsub('[^a-zA-Z0-9-/-: ]', '_')
7 end
8
9 function OnStableSeries(seriesId, tags, metadata)
10 print('This series is now stable, writing its instances on the disk: ' .. seriesId)
11
12 local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances']
13 local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags']
14 local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags']
15 local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags']
16
17 for i, instance in pairs(instances) do
18 local path = ToAscii(TARGET .. '/' ..
19 patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' ..
20 study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' ..
21 series['SeriesDescription'])
22
23 -- Retrieve the DICOM file from Orthanc
24 local dicom = RestApiGet('/instances/' .. instance .. '/file')
25
26 -- Create the subdirectory (CAUTION: For Linux demo only, this is insecure!)
27 -- http://stackoverflow.com/a/16029744/881731
28 os.execute('mkdir -p "' .. path .. '"')
29
30 -- Write to the file
31 local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb'))
32 target:write(dicom)
33 target:close()
34 end
35 end