-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGetCdrBlob.py
157 lines (150 loc) · 5.82 KB
/
GetCdrBlob.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/usr/bin/env python
# ----------------------------------------------------------------------
# Stream media blob over HTTP directly from the CDR database. We do
# this to avoid limitations imposed on memory usage within the CDR
# Server.
#
# BZIssue::4767
# ----------------------------------------------------------------------
import cdr
import sys
from lxml import etree
from cdrapi import db
from cdrcgi import Controller, FieldStorage
class DocInfo:
def __init__(self, cursor, docId, docVer=None):
self.docId = cdr.exNormalize(docId)[1]
self.docVer = docVer
self.mimeType = self.filename = None
if docVer:
cursor.execute("""\
SELECT xml
FROM doc_version
WHERE id = ?
AND num = ?""", (self.docId, self.docVer))
else:
cursor.execute("""\
SELECT xml
FROM document
WHERE id = ?""", self.docId)
rows = cursor.fetchall()
if not rows:
if docVer:
raise Exception("Cannot find version %s of CDR%s" %
(docVer, docId))
else:
raise Exception("Cannot find CDR%s" % docId)
try:
tree = etree.XML(rows[0][0])
except Exception:
try:
tree = etree.XML(rows[0][0].encode("utf-8"))
except Exception as e:
raise Exception("parsing CDR%s: %s" % (docId, e))
if tree.tag == 'Media':
for medium in ('Image', 'Sound', 'Video'):
xpath = 'PhysicalMedia/%sData/%sEncoding' % (medium, medium)
for e in tree.findall(xpath):
encoding = e.text
self.mimeType = {
'GIF': 'image/gif',
'JPEG': 'image/jpeg',
'PNG': 'image/png',
'MP3': 'audio/mpeg',
'RAM': 'audio/x-pn-realaudio',
'WAV': 'audio/x-wav',
'WMA': 'audio/x-ms-wma',
'AVI': 'video/x-msvideo',
'MPEG2': 'video/mpeg2',
'MJPG': 'video/x-motion-jpeg',
}.get(encoding)
suffix = {
'GIF': 'gif',
'JPEG': 'jpg',
'PNG': 'png',
'MP3': 'mp3',
'RAM': 'ram',
'WAV': 'wav',
'AVI': 'avi',
'MPEG2': 'mpeg',
'MJPG': 'mjpg',
}.get(encoding, 'bin')
self.filename = DocInfo.makeFilename(self.docId, docVer,
suffix)
elif tree.tag == 'SupplementaryInfo':
for e in tree.findall('MimeType'):
self.mimeType = e.text
for e in tree.findall('OriginalFilename'):
self.filename = e.text
if not self.filename:
suffix = {
'application/pdf': 'pdf',
'application/msword': 'doc',
'application/vnd.ms-excel': 'xls',
"application/vnd.openxlmformats-officedocument."
"spreadsheetml.sheet": "xlsx",
'application/vnd.wordperfect': 'wpd',
'application/vnd.ms-powerpoint': 'ppt',
'application/zip': 'zip',
'text/html': 'html',
'text/plain': 'txt',
'text/rtf': 'rtf',
'message/rfc822': 'eml',
'image/jpeg': 'jpg',
}.get(self.mimeType, 'bin')
self.filename = DocInfo.makeFilename(self.docId, docVer,
suffix)
else:
raise Exception("don't know about '%s' documents" % tree.tag)
if not self.mimeType:
if docVer:
raise Exception("unable to determine mime type for "
"version %s of CDR%d" %
(docVer, self.docId))
else:
raise Exception("unable to determine mime type for "
"CDR%d" % self.docId)
@staticmethod
def makeFilename(docId, docVer, suffix):
if docVer:
return "CDR%d-%s.%s" % (docId, docVer, suffix)
else:
return "CDR%d.%s" % (docId, suffix)
cursor = db.connect(user='CdrGuest').cursor()
fields = FieldStorage()
docId = fields.getvalue('id') or Controller.bail("Missing required 'id' parm")
docVer = fields.getvalue('ver') or ''
disp = fields.getvalue("disp") or "attachment"
try:
info = DocInfo(cursor, docId, docVer)
except Exception as e:
Controller.bail("%s" % e)
if info.docVer:
cursor.execute("""\
SELECT b.data
FROM doc_blob b
JOIN version_blob_usage u
ON u.blob_id = b.id
WHERE u.doc_id = %d
AND u.doc_version = %s""" % (info.docId, info.docVer))
else:
cursor.execute("""\
SELECT b.data
FROM doc_blob b
JOIN doc_blob_usage u
ON u.blob_id = b.id
WHERE u.doc_id = %d""" % info.docId)
rows = cursor.fetchall()
if not rows:
if info.docVer:
message = f"No blob found for version {docVer} of CDR{info.docId:d}"
Controller.bail(message)
else:
Controller.bail(f"No blob found for CDR document {info.docId:d}")
blob_bytes = rows[0][0]
sys.stdout.buffer.write(f"""\
Content-Type: {info.mimeType}
Content-Disposition: {disp}; filename={info.filename}
Content-Length: {len(blob_bytes)}
""".encode("utf-8"))
sys.stdout.buffer.write(blob_bytes)