|
a/Allura/allura/model/attachments.py |
|
b/Allura/allura/model/attachments.py |
|
... |
|
... |
9 |
from .session import project_orm_session
|
9 |
from .session import project_orm_session
|
10 |
from .filesystem import File
|
10 |
from .filesystem import File
|
11 |
|
11 |
|
12 |
class BaseAttachment(File):
|
12 |
class BaseAttachment(File):
|
13 |
thumbnail_size = (255, 255)
|
13 |
thumbnail_size = (255, 255)
|
|
|
14 |
ArtifactType=None
|
14 |
|
15 |
|
15 |
class __mongometa__:
|
16 |
class __mongometa__:
|
16 |
name = 'attachment.files'
|
17 |
name = 'attachment'
|
17 |
session = project_orm_session
|
18 |
session = project_orm_session
|
18 |
indexes = ['metadata.filename']
|
19 |
indexes = [ 'artifact_id', 'app_config_id' ]
|
19 |
|
20 |
|
20 |
# Override the metadata schema here
|
21 |
artifact_id=FieldProperty(S.ObjectId)
|
21 |
metadata=FieldProperty(dict(
|
22 |
app_config_id=FieldProperty(S.ObjectId)
|
22 |
artifact_id=S.ObjectId,
|
23 |
type=FieldProperty(str)
|
23 |
app_config_id=S.ObjectId,
|
|
|
24 |
type=str,
|
|
|
25 |
filename=str))
|
|
|
26 |
|
24 |
|
27 |
@property
|
25 |
@property
|
28 |
def artifact(self):
|
26 |
def artifact(self):
|
29 |
raise NotImplementedError, 'artifact'
|
27 |
return self.ArtifactType.query.get(_id=self.artifact_id)
|
30 |
|
28 |
|
31 |
def url(self):
|
29 |
def url(self):
|
32 |
return self.artifact.url() + 'attachment/' + self.filename
|
30 |
return self.artifact.url() + 'attachment/' + self.filename
|
33 |
|
31 |
|
34 |
def is_embedded(self):
|
32 |
def is_embedded(self):
|
35 |
from pylons import request
|
33 |
from pylons import request
|
36 |
return self.metadata.filename in request.environ.get('allura.macro.att_embedded', [])
|
34 |
return self.filename in request.environ.get('allura.macro.att_embedded', [])
|
37 |
|
35 |
|
38 |
@classmethod
|
36 |
@classmethod
|
39 |
def metadata_for(cls, artifact):
|
37 |
def metadata_for(cls, artifact):
|
40 |
return dict(
|
38 |
return dict(
|
41 |
artifact_id=artifact._id,
|
39 |
artifact_id=artifact._id,
|
42 |
app_config_id=artifact.app_config_id)
|
40 |
app_config_id=artifact.app_config_id)
|
43 |
|
41 |
|
44 |
@classmethod
|
42 |
@classmethod
|
45 |
def save_attachment(cls, filename, fp, **kwargs):
|
43 |
def save_attachment(cls, filename, fp, content_type=None, **kwargs):
|
46 |
content_type = kwargs.pop('content_type', None)
|
|
|
47 |
if content_type is None:
|
|
|
48 |
content_type = guess_type(filename)
|
|
|
49 |
if content_type[0]: content_type = content_type[0]
|
|
|
50 |
else: content_type = 'application/octet-stream'
|
|
|
51 |
|
|
|
52 |
thumbnail_meta = dict(type="thumbnail", app_config_id=c.app.config._id)
|
44 |
thumbnail_meta = dict(type="thumbnail", app_config_id=c.app.config._id)
|
53 |
thumbnail_meta.update(kwargs)
|
45 |
thumbnail_meta.update(kwargs)
|
54 |
original_meta = dict(type="attachment", app_config_id=c.app.config._id)
|
46 |
original_meta = dict(type="attachment", app_config_id=c.app.config._id)
|
55 |
original_meta.update(kwargs)
|
47 |
original_meta.update(kwargs)
|
56 |
# Try to save as image, with thumbnail
|
48 |
# Try to save as image, with thumbnail
|
57 |
orig, thumbnail = cls.save_image(
|
49 |
orig, thumbnail = cls.save_image(
|
|
|
50 |
filename, fp,
|
58 |
filename, fp, content_type=content_type,
|
51 |
content_type=content_type,
|
59 |
square=True, thumbnail_size=cls.thumbnail_size,
|
52 |
square=True, thumbnail_size=cls.thumbnail_size,
|
60 |
thumbnail_meta=thumbnail_meta,
|
53 |
thumbnail_meta=thumbnail_meta,
|
61 |
save_original=True,
|
54 |
save_original=True,
|
62 |
original_meta=original_meta)
|
55 |
original_meta=original_meta)
|
63 |
if orig is not None:
|
56 |
if orig is not None:
|
64 |
return orig, thumbnail
|
57 |
return orig, thumbnail
|
65 |
|
58 |
|
66 |
# No, generic attachment
|
59 |
# No, generic attachment
|
67 |
with cls.create(
|
60 |
return cls.from_stream(
|
68 |
content_type=content_type,
|
61 |
filename, fp, content_type=content_type,
|
69 |
filename=filename,
|
|
|
70 |
**original_meta) as fp_w:
|
62 |
**original_meta)
|
71 |
fp_w.write(fp.read())
|
|
|
72 |
orig = cls.query.get(filename=fp_w.name)
|
|
|
73 |
return orig, None
|
|
|
74 |
|
63 |
|