Anonymizing files with lua script before autorouting.

Hello,

Today I have been working on autorouting in my local Orthanc server, this server should send files to the online Orthanc server.

I managed to get autorouting to work with the following script:

"
function OnStoredInstance(instanceId, tags, metadata, origin)
SendToPeer(instanceId, ‘internet’)
end
"

However I got blocked when trying to anonymize the image. I’m hitting the ‘instances/{id}/anonymize’ rest function through RestApiPost, I can’t recieve the image or tags in Lua

Here is my code now:
"
function OnStoredInstance(instanceId, tags, metadata, origin)

anonJson = ‘{“Replace”:{“PatientName”:“Anónimo”},“Keep”:[“StudyDate”,“AccessionNumber”]}’
anonUri = ‘/instances/’ … instanceId … ‘/anonymize’
local anonymized = RestApiPost(anonUri, ‘{}’)
local dumpanonymized = DumpJson(anonymized,true)
print(anonymized)
print(dumpanonymized)
PrintRecursive(anonymized)
PrintRecursive(dumpanonymized)
RestApiPost(‘/peers/internet/store’,anonymized,true)
print(‘forwarded an instance’)
end

And this is the output with --verbose and --trace

"

T0313 21:10:13.635822 HttpServer.cpp:652] HTTP header: [connection]: [keep-alive]
I0313 21:10:13.638713 FilesystemStorage.cpp:118] Creating attachment “f55eb46b-59ad-4f1d-8ff1-533aa0378c45” of “DICOM” type (size: 1MB)
I0313 21:10:13.641957 HttpServer.cpp:788] POST /instances
I0313 21:10:13.642022 OrthancRestApi.cpp:118] Receiving a DICOM file of 526530 bytes through HTTP
I0313 21:10:13.642503 FilesystemStorage.cpp:118] Creating attachment “72a42f69-e1e1-4302-a0df-98b4dd3c944b” of “JSON summary of DICOM” type (size: 1MB)
T0313 21:10:13.643190 ServerIndex.cpp:199] Change related to resource 73521d7b-4846cc12-414e49dd-0f7d62c7-8f1939e8 of type Instance: NewInstance
T0313 21:10:13.643238 ServerIndex.cpp:199] Change related to resource 287815b1-26105046-4a1d7f6f-81dfe0b9-074a33f2 of type Series: NewSeries
T0313 21:10:13.643595 ServerIndex.cpp:199] Change related to resource 287815b1-26105046-4a1d7f6f-81dfe0b9-074a33f2 of type Series: NewChildInstance
T0313 21:10:13.643628 ServerIndex.cpp:199] Change related to resource a3d17d72-7a55f039-b3f41e8a-9b2fdaab-2d5499db of type Study: NewChildInstance
T0313 21:10:13.643653 ServerIndex.cpp:199] Change related to resource dcad0624-d43d9d84-161d8e5d-7527b92f-b540a654 of type Patient: NewChildInstance
I0313 21:10:13.644074 ServerContext.cpp:417] New instance stored
T0313 21:10:13.644466 HttpOutput.cpp:382] Compressing a HTTP answer using gzip
T0313 21:10:13.644810 MemoryCache.cpp:57] Dropping the oldest cache page
I0313 21:10:13.645231 FilesystemStorage.cpp:155] Reading attachment “f55eb46b-59ad-4f1d-8ff1-533aa0378c45” of “DICOM” content type
T0313 21:10:13.646341 MemoryCache.cpp:68] Registering new data in a cache page
T0313 21:10:13.646805 MemoryCache.cpp:86] Invalidating a cache page
I0313 21:10:13.648406 FilesystemStorage.cpp:118] Creating attachment “a499771c-f218-488e-87e5-680b9ad1925b” of “DICOM” type (size: 1MB)
W0313 21:10:13.649109 LuaContext.cpp:103] Lua says:
W0313 21:10:13.649651 LuaContext.cpp:103] Lua says: “”

W0313 21:10:13.650165 LuaContext.cpp:103] Lua says: string,
I0313 21:10:13.650338 FilesystemStorage.cpp:118] Creating attachment “2967a116-f800-4439-99ca-0bd7a64033d5” of “JSON summary of DICOM” type (size: 1MB)
W0313 21:10:13.650306 LuaContext.cpp:103] Lua says: string, “”
"

From what I can figure, the server is receiving the image, receiving the anonymize request, sending the anonymized response (creating attachment), but it’s not picked up correctly by the Lua context.

I have managed to compile Orthanc with Debug symbols, so tomorrow I might be able to debug this further, but I would appreciate some help here.

Thank you for your help.

Update:

I can succesfully anonymize a file via python.
I thought at one point that the reason this was failing was that the dicom-web plugin wasn’t activated. So I changed the config but it didn’t change the error message to my surprise, maybe it’s activated by default?

I’ll post the minimum example of the bug:

`

function OnStoredInstance(instanceId, tags, metadata, origin)
anonUri = ‘/instances/’ … instanceId … ‘/anonymize’
local anonymized = RestApiPost(anonUri, ‘{}’ )
print(anonymized)
end

`

RestApiPost simply returns an empty string. The expected result would be an instance of an image without most tags.

The following python code works, it returns http code 200 and the full 512kb image. (The same image that failed in the same local Orthanc):

`
import requests
import pprint

a = requests.post(‘http://localhost:8042/instances/48b858cf-8542dc7f-64b012d9-f173d1c8-05b69df9/anonymize’,'{}’,auth=auth)
pprint.pprint(vars(a))

`

I’m going to rely on an external service (python) to consume the rest api, it’s a shame that I won’t have events or notifications, but I guess I can poll Orthanc frequently and respond to events in this manner, like so https://bitbucket.org/sjodogne/orthanc/src/default/Resources/Samples/Python/AutoClassify.py

I tried a pure HttpPost call in Lua but I got another error. If I were to continue trying I would try to manually encode the user and password for the http auth headers, in case this curl implementation doesn’t support auth through url.:

Code:

`
function OnStoredInstance(instanceId, tags, metadata, origin)
anonUri = ‘/instances/’ … instanceId … ‘/anonymize’
local auth = ‘user:password’
local anonymized = HttpPost(‘http://’ … auth … ‘@localhost:8042’ … anonUri, ‘{}’)
local dumpanonymized = DumpJson(anonymized,true)
print(anonymized)
print(dumpanonymized)
PrintRecursive(anonymized)
PrintRecursive(dumpanonymized)
end

`

In your first script, the “problem” is simply that print(anonymized) does not print anything because printing a huge byte array does not make a lot of sense (I don’t know if this behaviour comes from Lua itself or from Orthanc).

But, you can actually try this code and it works:
print(" original instance id : " … instanceId)

local modifiedFile = RestApiPost(‘/instances/’ … instanceId … ‘/anonymize’, ‘{}’)
print(" anonymized file : " … modifiedFile)
– RestApiDelete(‘/instances/’ … instanceId)
local modifiedId = ParseJson(RestApiPost(‘/instances/’, modifiedFile)) [‘ID’]
print(" anonymized instance id : " … modifiedId)