Don't bother returning whether or not we copied it or not, we can
[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
894facc6 21from mediagoblin import globals as mg_globals
41f446f4
CAW
22
23
24THUMB_SIZE = 200, 200
25
26
27@task
28def process_media_initial(media_id):
894facc6 29 entry = mg_globals.database.MediaEntry.one(
254bc431 30 {'_id': ObjectId(media_id)})
41f446f4 31
fa7f9c61 32 queued_filepath = entry['queued_media_file']
894facc6 33 queued_file = mg_globals.queue_store.get_file(queued_filepath, 'r')
41f446f4
CAW
34
35 with queued_file:
fa7f9c61 36 thumb = Image.open(queued_file)
41f446f4
CAW
37 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
38
894facc6 39 thumb_filepath = mg_globals.public_store.get_unique_filepath(
41f446f4
CAW
40 ['media_entries',
41 unicode(entry['_id']),
42 'thumbnail.jpg'])
43
894facc6
CAW
44 thumb_file = mg_globals.public_store.get_file(thumb_filepath, 'w')
45 with thumb_file:
41f446f4
CAW
46 thumb.save(thumb_file, "JPEG")
47
fa7f9c61
CAW
48 # we have to re-read because unlike PIL, not everything reads
49 # things in string representation :)
894facc6 50 queued_file = mg_globals.queue_store.get_file(queued_filepath, 'rb')
fa7f9c61
CAW
51
52 with queued_file:
894facc6 53 main_filepath = mg_globals.public_store.get_unique_filepath(
fa7f9c61
CAW
54 ['media_entries',
55 unicode(entry['_id']),
56 queued_filepath[-1]])
57
894facc6 58 with mg_globals.public_store.get_file(main_filepath, 'wb') as main_file:
fa7f9c61
CAW
59 main_file.write(queued_file.read())
60
894facc6 61 mg_globals.queue_store.delete_file(queued_filepath)
fa7f9c61
CAW
62 media_files_dict = entry.setdefault('media_files', {})
63 media_files_dict['thumb'] = thumb_filepath
64 media_files_dict['main'] = main_filepath
12b6ecac
CAW
65 entry['state'] = u'processed'
66 entry.save()