1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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.
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.
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/>.
18 from mediagoblin
.db
.util
import ObjectId
19 from celery
.task
import task
21 from mediagoblin
import mg_globals
as mgg
28 def process_media_initial(media_id
):
29 workbench
= mgg
.workbench_manager
.create_workbench()
31 entry
= mgg
.database
.MediaEntry
.one(
32 {'_id': ObjectId(media_id
)})
34 queued_filepath
= entry
['queued_media_file']
35 queued_filename
= workbench
.localized_file(
36 mgg
.queue_store
, queued_filepath
,
39 queued_file
= file(queued_filename
, 'r')
42 thumb
= Image
.open(queued_file
)
43 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
44 # ensure color mode is compatible with jpg
45 if thumb
.mode
!= "RGB":
46 thumb
= thumb
.convert("RGB")
48 thumb_filepath
= mgg
.public_store
.get_unique_filepath(
50 unicode(entry
['_id']),
53 thumb_file
= mgg
.public_store
.get_file(thumb_filepath
, 'w')
55 thumb
.save(thumb_file
, "JPEG")
57 # we have to re-read because unlike PIL, not everything reads
58 # things in string representation :)
59 queued_file
= file(queued_filename
, 'rb')
62 main_filepath
= mgg
.public_store
.get_unique_filepath(
64 unicode(entry
['_id']),
67 with mgg
.public_store
.get_file(main_filepath
, 'wb') as main_file
:
68 main_file
.write(queued_file
.read())
70 mgg
.queue_store
.delete_file(queued_filepath
)
71 media_files_dict
= entry
.setdefault('media_files', {})
72 media_files_dict
['thumb'] = thumb_filepath
73 media_files_dict
['main'] = main_filepath
74 entry
['state'] = u
'processed'
78 workbench
.destroy_self()