|
a/Allura/allura/controllers/attachments.py |
|
b/Allura/allura/controllers/attachments.py |
|
... |
|
... |
35 |
self.artifact = artifact
|
35 |
self.artifact = artifact
|
36 |
|
36 |
|
37 |
@LazyProperty
|
37 |
@LazyProperty
|
38 |
def attachment(self):
|
38 |
def attachment(self):
|
39 |
metadata = self.AttachmentClass.metadata_for(self.artifact)
|
39 |
metadata = self.AttachmentClass.metadata_for(self.artifact)
|
40 |
metadata_query = dict(
|
40 |
metadata['type'] = 'attachment'
|
41 |
('metadata.%s' % k, v)
|
|
|
42 |
for k, v in metadata.iteritems())
|
|
|
43 |
attachment = self.AttachmentClass.query.get(filename=self.filename, **metadata_query)
|
41 |
attachment = self.AttachmentClass.query.get(filename=self.filename, **metadata)
|
44 |
if attachment is None:
|
|
|
45 |
attachment = self.AttachmentClass.by_metadata(filename=self.filename, **metadata).first()
|
|
|
46 |
if attachment is None:
|
42 |
if attachment is None:
|
47 |
raise exc.HTTPNotFound
|
43 |
raise exc.HTTPNotFound
|
48 |
return attachment
|
44 |
return attachment
|
49 |
|
45 |
|
50 |
@LazyProperty
|
46 |
@LazyProperty
|
51 |
def thumbnail(self):
|
47 |
def thumbnail(self):
|
52 |
thumbnail = self.AttachmentClass.by_metadata(filename=self.attachment.filename).first()
|
48 |
metadata = self.AttachmentClass.metadata_for(self.artifact)
|
|
|
49 |
metadata['type'] = 'thumbnail'
|
|
|
50 |
attachment = self.AttachmentClass.query.get(filename=self.filename, **metadata)
|
53 |
if thumbnail is None:
|
51 |
if attachment is None:
|
54 |
raise exc.HTTPNotFound
|
52 |
raise exc.HTTPNotFound
|
55 |
return thumbnail
|
53 |
return attachment
|
56 |
|
54 |
|
57 |
@expose()
|
55 |
@expose()
|
58 |
def index(self, delete=False, embed=True, **kw):
|
56 |
def index(self, delete=False, embed=True, **kw):
|
59 |
if request.method == 'POST':
|
57 |
if request.method == 'POST':
|
60 |
require(has_artifact_access(self.edit_perm, self.artifact))
|
58 |
require(has_artifact_access(self.edit_perm, self.artifact))
|
|
... |
|
... |
64 |
if self.thumbnail:
|
62 |
if self.thumbnail:
|
65 |
self.thumbnail.delete()
|
63 |
self.thumbnail.delete()
|
66 |
except exc.HTTPNotFound:
|
64 |
except exc.HTTPNotFound:
|
67 |
pass
|
65 |
pass
|
68 |
redirect(request.referer)
|
66 |
redirect(request.referer)
|
69 |
with self.attachment.open() as fp:
|
67 |
return self.attachment.serve(embed)
|
70 |
if fp is None:
|
|
|
71 |
raise exc.HTTPNotFound()
|
|
|
72 |
filename = fp.metadata['filename'].encode('utf-8')
|
|
|
73 |
if fp.content_type is None:
|
|
|
74 |
fp.content_type = 'application/octet-stream'
|
|
|
75 |
response.headers['Content-Type'] = fp.content_type.encode('utf-8')
|
|
|
76 |
response.content_type = fp.content_type.encode('utf-8')
|
|
|
77 |
if not embed:
|
|
|
78 |
response.headers.add('Content-Disposition',
|
|
|
79 |
'attachment;filename="%s"' % filename)
|
|
|
80 |
else:
|
|
|
81 |
response.headers.add('Content-Disposition',
|
|
|
82 |
'filename="%s"' % filename)
|
|
|
83 |
return fp.read()
|
|
|
84 |
return self.filename
|
|
|
85 |
|
68 |
|
86 |
@expose()
|
69 |
@expose()
|
87 |
def thumb(self, embed=True):
|
70 |
def thumb(self, embed=True):
|
88 |
with self.thumbnail.open() as fp:
|
71 |
return self.thumbnail.serve(embed)
|
89 |
filename = fp.metadata['filename'].encode('utf-8')
|
|
|
90 |
response.headers['Content-Type'] = ''
|
|
|
91 |
response.content_type = fp.content_type.encode('utf-8')
|
|
|
92 |
if not embed:
|
|
|
93 |
response.headers.add('Content-Disposition',
|
|
|
94 |
'attachment;filename=%s' % filename)
|
|
|
95 |
return fp.read()
|
|
|
96 |
return self.filename
|
|
|