Mercurial > hg > orthanc-book
annotate Sphinx/source/plugins/python/storage-area.py @ 748:a296fe06fd86
Implementing a custom storage area in Python
author | Sebastien Jodogne <s.jodogne@gmail.com> |
---|---|
date | Thu, 12 Aug 2021 17:51:54 +0200 |
parents | |
children |
rev | line source |
---|---|
748
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
1 import orthanc |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
2 import os |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
3 |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
4 def GetPath(uuid, contentType): |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
5 # Returns the path where to store the given attachment |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
6 return 'attachment-%d-%s' % (contentType, uuid) |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
7 |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
8 def OnCreate(uuid, contentType, data): |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
9 with open(GetPath(uuid, contentType), 'wb') as f: |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
10 f.write(data) |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
11 |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
12 def OnRead(uuid, contentType): |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
13 with open(GetPath(uuid, contentType), 'rb') as f: |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
14 return f.read() |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
15 |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
16 def OnRemove(uuid, contentType): |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
17 os.remove(GetPath(uuid, contentType)) |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
18 |
a296fe06fd86
Implementing a custom storage area in Python
Sebastien Jodogne <s.jodogne@gmail.com>
parents:
diff
changeset
|
19 orthanc.RegisterStorageArea(OnCreate, OnRead, OnRemove) |