Set the jpeg quality at 90 for now...
[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
23
24 THUMB_SIZE = 180, 180
25 MEDIUM_SIZE = 640, 640
26
27
28 def create_pub_filepath(entry, filename):
29 return mgg.public_store.get_unique_filepath(
30 ['media_entries',
31 unicode(entry['_id']),
32 filename])
33
34
35 @task
36 def process_media_initial(media_id):
37 workbench = mgg.workbench_manager.create_workbench()
38
39 entry = mgg.database.MediaEntry.one(
40 {'_id': ObjectId(media_id)})
41
42 queued_filepath = entry['queued_media_file']
43 queued_filename = workbench.localized_file(
44 mgg.queue_store, queued_filepath,
45 'source')
46
47 thumb = Image.open(queued_filename)
48 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
49 # ensure color mode is compatible with jpg
50 if thumb.mode != "RGB":
51 thumb = thumb.convert("RGB")
52
53 thumb_filepath = create_pub_filepath(entry, 'thumbnail.jpg')
54
55 thumb_file = mgg.public_store.get_file(thumb_filepath, 'w')
56 with thumb_file:
57 thumb.save(thumb_file, "JPEG", quality=90)
58
59 """
60 Create medium file, used in `media.html`
61 """
62 medium = Image.open(queued_filename)
63 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
64
65 if medium.mode != "RGB":
66 medium = medium.convert("RGB")
67
68 medium_filepath = create_pub_filepath(entry, 'medium.jpg')
69
70 medium_file = mgg.public_store.get_file(medium_filepath, 'w')
71 with medium_file:
72 medium.save(medium_file, "JPEG", quality=90)
73
74 # we have to re-read because unlike PIL, not everything reads
75 # things in string representation :)
76 queued_file = file(queued_filename, 'rb')
77
78 with queued_file:
79 main_filepath = create_pub_filepath(entry, queued_filepath[-1])
80
81 with mgg.public_store.get_file(main_filepath, 'wb') as main_file:
82 main_file.write(queued_file.read())
83
84 mgg.queue_store.delete_file(queued_filepath)
85 entry['queued_media_file'] = []
86 media_files_dict = entry.setdefault('media_files', {})
87 media_files_dict['thumb'] = thumb_filepath
88 media_files_dict['main'] = main_filepath
89 media_files_dict['medium'] = medium_filepath
90 entry['state'] = u'processed'
91 entry.save()
92
93 # clean up workbench
94 workbench.destroy_self()