orthancFileBrowser.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import urllib3
  2. import shutil
  3. class orthancFileBrowser:
  4. def __init__(self,net):
  5. self.net=net
  6. def getZip(self,retrieveLevel, dataId, path, mode='archive'):
  7. #mode can be archive(ZIP,default) or media (DICOMDIR)
  8. url=self.net.getCoreURL()
  9. url+="/"+retrieveLevel+"/"+dataId+"/"+mode
  10. print("Using: {}".format(url))
  11. response=self.net.get(url,binary=True)
  12. with open(path,'wb') as out_file:
  13. shutil.copyfileobj(response,out_file)
  14. response.release_conn()
  15. #response.data is a byte array -> is the same as file
  16. def getInstance(self, instanceId, localFile):
  17. url=self.net.getCoreURL()
  18. url+='/instances/'+instanceId+'/file'
  19. print('Using {}'.format(url))
  20. response=self.net.get(url,binary=True)
  21. with open(localFile,'wb') as out_file:
  22. shutil.copyfileobj(response,out_file)
  23. response.release_conn()
  24. def upload(self,path):
  25. url=self.net.getCoreURL()
  26. url+="/instances"
  27. with open(path,'rb') as f:
  28. response=self.net.post(url,"octet-stream", f.read())
  29. print(response.data)