Merge remote branch 'upstream/master' into dev/mount_storage
[mediagoblin.git] / mediagoblin / process_media / __init__.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17 import Image
18 from mediagoblin.db.util import ObjectId
19 from celery.task import task
20
21 from mediagoblin import mg_globals as mgg
22 from contextlib import contextmanager
23
24
25 THUMB_SIZE = 180, 180
26 MEDIUM_SIZE = 640, 640
27
28
29 def create_pub_filepath(entry, filename):
30 return mgg.public_store.get_unique_filepath(
31 ['media_entries',
32 unicode(entry['_id']),
33 filename])
34
35 @contextmanager
36 def closing(callback):
37 try:
38 yield callback
39 finally:
40 pass
41
42 @task
43 def process_media_initial(media_id):
44 workbench = mgg.workbench_manager.create_workbench()
45
46 entry = mgg.database.MediaEntry.one(
47 {'_id': ObjectId(media_id)})
48
49 queued_filepath = entry['queued_media_file']
50 queued_filename = workbench.localized_file(
51 mgg.queue_store, queued_filepath,
52 'source')
53
54 thumb = Image.open(queued_filename)
55 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
56 # ensure color mode is compatible with jpg
57 if thumb.mode != "RGB":
58 thumb = thumb.convert("RGB")
59
60 thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
61
62 thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
63 with closing(thumb_file):
64 thumb.save(thumb_file, "JPEG", quality=90)
65
66 """
67 If the size of the original file exceeds the specified size of a `medium`
68 file, a `medium.jpg` files is created and later associated with the media
69 entry.
70 """
71 medium = Image.open(queued_filename)
72 medium_processed = False
73
74 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
75 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
76
77 if medium.mode != "RGB":
78 medium = medium.convert("RGB")
79
80 medium_filepath = create_pub_filepath(entry, 'medium.jpg')
81
82 medium_file = mgg.public_store.get_file(medium_filepath, 'w')
83 with closing(medium_file):
84 medium.save(medium_file, "JPEG", quality=90)
85 medium_processed = True
86
87 # we have to re-read because unlike PIL, not everything reads
88 # things in string representation :)
89 queued_file = file(queued_filename, 'rb')
90
91 with queued_file:
92 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
93
94 with closing(mgg.public_store.get_file(original_filepath, 'wb')) as original_file:
95 original_file.write(queued_file.read())
96
97 mgg.queue_store.delete_file(queued_filepath)
98 entry['queued_media_file'] = []
99 media_files_dict = entry.setdefault('media_files', {})
100 media_files_dict['thumb'] = thumb_filepath
101 media_files_dict['original'] = original_filepath
102 if medium_processed:
103 media_files_dict['medium'] = medium_filepath
104 entry['state'] = u'processed'
105 entry.save()
106
107 # clean up workbench
108 workbench.destroy_self()