Ok, so here are the actual changes. Woops!
[mediagoblin.git] / mediagoblin / process_media / __init__.py
CommitLineData
41f446f4
CAW
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
17import Image
254bc431 18from mediagoblin.db.util import ObjectId
41f446f4
CAW
19from celery.task import task
20
300c34e8 21from mediagoblin import mg_globals as mgg
41f446f4
CAW
22
23
24eaf0fd 24THUMB_SIZE = 180, 180
93214d8e 25MEDIUM_SIZE = 640, 640
41f446f4
CAW
26
27
180bdbde 28def create_pub_filepath(entry, filename):
48a7ba1e 29 return mgg.public_store.get_unique_filepath(
180bdbde
E
30 ['media_entries',
31 unicode(entry['_id']),
32 filename])
33
34
41f446f4
CAW
35@task
36def process_media_initial(media_id):
300c34e8 37 workbench = mgg.workbench_manager.create_workbench()
ca030ab6 38
300c34e8 39 entry = mgg.database.MediaEntry.one(
254bc431 40 {'_id': ObjectId(media_id)})
41f446f4 41
fa7f9c61 42 queued_filepath = entry['queued_media_file']
52426ae0
E
43 queued_filename = workbench.localized_file(
44 mgg.queue_store, queued_filepath,
ca030ab6
CAW
45 'source')
46
93214d8e
JW
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")
41f446f4 52
93214d8e
JW
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:
899d8916 57 thumb.save(thumb_file, "JPEG", quality=90)
93214d8e
JW
58
59 """
2c9e635a
JW
60 If the size of the original file exceeds the specified size of a `medium`
61 file, a `medium.jpg` files is created and later associated with the media
62 entry.
93214d8e
JW
63 """
64 medium = Image.open(queued_filename)
2c9e635a 65 medium_processed = False
93214d8e 66
2c9e635a
JW
67 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
68 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
41f446f4 69
2c9e635a
JW
70 if medium.mode != "RGB":
71 medium = medium.convert("RGB")
41f446f4 72
2c9e635a
JW
73 medium_filepath = create_pub_filepath(entry, 'medium.jpg')
74
75 medium_file = mgg.public_store.get_file(medium_filepath, 'w')
76 with medium_file:
5f72a4c3 77 medium.save(medium_file, "JPEG", quality=90)
2c9e635a 78 medium_processed = True
41f446f4 79
fa7f9c61
CAW
80 # we have to re-read because unlike PIL, not everything reads
81 # things in string representation :)
ca030ab6 82 queued_file = file(queued_filename, 'rb')
fa7f9c61
CAW
83
84 with queued_file:
2c9e635a 85 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
fa7f9c61 86
2c9e635a
JW
87 with mgg.public_store.get_file(original_filepath, 'wb') as original_file:
88 original_file.write(queued_file.read())
fa7f9c61 89
300c34e8 90 mgg.queue_store.delete_file(queued_filepath)
180bdbde 91 entry['queued_media_file'] = []
fa7f9c61
CAW
92 media_files_dict = entry.setdefault('media_files', {})
93 media_files_dict['thumb'] = thumb_filepath
2c9e635a
JW
94 media_files_dict['original'] = original_filepath
95 if medium_processed:
96 media_files_dict['medium'] = medium_filepath
12b6ecac
CAW
97 entry['state'] = u'processed'
98 entry.save()
ca030ab6
CAW
99
100 # clean up workbench
b67a983a 101 workbench.destroy_self()