Add a rudimentary media processing function.
[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
18import mongokit
19from celery.task import task
20
21from mediagoblin.globals import database, queue_store, public_store
22
23
24THUMB_SIZE = 200, 200
25
26
27@task
28def process_media_initial(media_id):
29 entry = database.MediaEntry.one(
30 {'_id': mongokit.ObjectId(media_id)})
31
32 queued_filepath = entry['queue_files'].pop()
33 queued_file = queue_store.get_file(queued_filepath, 'r')
34
35 with queued_file:
36 thumb = Image(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 queue_store.delete(queued_filepath)
48 entry.setdefault('media_files', []).append(thumb_filepath)
49 entry.state = 'processed'
50 entry.save()