[to_email] rather than list(to_email) which makes a nasty series like ['e','m','a...
[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 import mongokit
19 from celery.task import task
20
21 from mediagoblin.globals import database, queue_store, public_store
22
23
24 THUMB_SIZE = 200, 200
25
26
27 @task
28 def process_media_initial(media_id):
29 entry = database.MediaEntry.one(
30 {'_id': mongokit.ObjectId(media_id)})
31
32 queued_filepath = entry['queued_media_file']
33 queued_file = queue_store.get_file(queued_filepath, 'r')
34
35 with queued_file:
36 thumb = Image.open(queued_file)
37 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
38
39 thumb_filepath = public_store.get_unique_filepath(
40 ['media_entries',
41 unicode(entry['_id']),
42 'thumbnail.jpg'])
43
44 with public_store.get_file(thumb_filepath, 'w') as thumb_file:
45 thumb.save(thumb_file, "JPEG")
46
47 # we have to re-read because unlike PIL, not everything reads
48 # things in string representation :)
49 queued_file = queue_store.get_file(queued_filepath, 'rb')
50
51 with queued_file:
52 main_filepath = public_store.get_unique_filepath(
53 ['media_entries',
54 unicode(entry['_id']),
55 queued_filepath[-1]])
56
57 with public_store.get_file(main_filepath, 'wb') as main_file:
58 main_file.write(queued_file.read())
59
60 queue_store.delete_file(queued_filepath)
61 media_files_dict = entry.setdefault('media_files', {})
62 media_files_dict['thumb'] = thumb_filepath
63 media_files_dict['main'] = main_filepath
64 entry['state'] = u'processed'
65 entry.save()