# HG changeset patch # User Sebastien Jodogne # Date 1438011227 -7200 # Node ID 905842836ad406d0afbe097d2e788a3c315270f2 # Parent f401dd90b35e449271a528856032b8fd8e9bf296 sample Lua script to write DICOM series to disk diff -r f401dd90b35e -r 905842836ad4 Core/Lua/LuaContext.cpp --- a/Core/Lua/LuaContext.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/Core/Lua/LuaContext.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -137,7 +137,7 @@ Json::FastWriter writer; std::string s = writer.write(json); - lua_pushstring(state, s.c_str()); + lua_pushlstring(state, s.c_str(), s.size()); return 1; } @@ -181,7 +181,7 @@ } // Return the result of the HTTP request - lua_pushstring(state, str.c_str()); + lua_pushlstring(state, str.c_str(), str.size()); return true; } @@ -307,7 +307,8 @@ { if (value.isString()) { - lua_pushstring(lua_, value.asCString()); + const std::string s = value.asString(); + lua_pushlstring(lua_, s.c_str(), s.size()); } else if (value.isDouble()) { @@ -356,7 +357,7 @@ it = members.begin(); it != members.end(); ++it) { // Push the index of the cell - lua_pushstring(lua_, it->c_str()); + lua_pushlstring(lua_, it->c_str(), it->size()); // Push the value of the cell PushJson(value[*it]); diff -r f401dd90b35e -r 905842836ad4 Core/Lua/LuaFunctionCall.cpp --- a/Core/Lua/LuaFunctionCall.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/Core/Lua/LuaFunctionCall.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -61,7 +61,7 @@ void LuaFunctionCall::PushString(const std::string& value) { CheckAlreadyExecuted(); - lua_pushstring(context_.lua_, value.c_str()); + lua_pushlstring(context_.lua_, value.c_str(), value.size()); } void LuaFunctionCall::PushBoolean(bool value) diff -r f401dd90b35e -r 905842836ad4 OrthancServer/LuaScripting.cpp --- a/OrthancServer/LuaScripting.cpp Fri Jul 03 10:03:40 2015 +0200 +++ b/OrthancServer/LuaScripting.cpp Mon Jul 27 17:33:47 2015 +0200 @@ -86,7 +86,7 @@ std::string result; if (HttpToolbox::SimpleGet(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri)) { - lua_pushstring(state, result.c_str()); + lua_pushlstring(state, result.c_str(), result.size()); } else { @@ -133,7 +133,7 @@ HttpToolbox::SimplePut(result, serverContext->GetHttpHandler().RestrictToOrthancRestApi(builtin), uri, bodyData, bodySize)) { - lua_pushstring(state, result.c_str()); + lua_pushlstring(state, result.c_str(), result.size()); } else { diff -r f401dd90b35e -r 905842836ad4 Resources/Samples/Lua/WriteToDisk.lua --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Resources/Samples/Lua/WriteToDisk.lua Mon Jul 27 17:33:47 2015 +0200 @@ -0,0 +1,34 @@ +TARGET = '/tmp/lua' + +function ToAscii(s) + -- http://www.lua.org/manual/5.1/manual.html#pdf-string.gsub + return s:gsub('[^a-zA-Z0-9-/ ]', '_') +end + +function OnStableSeries(seriesId, tags, metadata) + print('This series is now stable, writing its instances on the disk: ' .. seriesId) + + local instances = ParseJson(RestApiGet('/series/' .. seriesId)) ['Instances'] + local patient = ParseJson(RestApiGet('/series/' .. seriesId .. '/patient')) ['MainDicomTags'] + local study = ParseJson(RestApiGet('/series/' .. seriesId .. '/study')) ['MainDicomTags'] + local series = ParseJson(RestApiGet('/series/' .. seriesId)) ['MainDicomTags'] + + for i, instance in pairs(instances) do + local path = ToAscii(TARGET .. '/' .. + patient['PatientID'] .. ' - ' .. patient['PatientName'] .. '/' .. + study['StudyDate'] .. ' - ' .. study['StudyDescription'] .. '/' .. + series['SeriesDescription']) + + -- 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 .. '"') + + -- Write to the file + local target = assert(io.open(path .. '/' .. instance .. '.dcm', 'wb')) + target:write(dicom) + target:close() + end +end