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
22 from contextlib
import contextmanager
26 MEDIUM_SIZE
= 640, 640
29 def create_pub_filepath(entry
, filename
):
30 return mgg
.public_store
.get_unique_filepath(
32 unicode(entry
['_id']),
36 def closing(callback
):
43 def process_media_initial(media_id
):
44 workbench
= mgg
.workbench_manager
.create_workbench()
46 entry
= mgg
.database
.MediaEntry
.one(
47 {'_id': ObjectId(media_id
)})
49 queued_filepath
= entry
['queued_media_file']
50 queued_filename
= workbench
.localized_file(
51 mgg
.queue_store
, queued_filepath
,
54 thumb
= Image
.open(queued_filename
)
55 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
56 # ensure color mode is compatible with jpg
57 if thumb
.mode
!= "RGB":
58 thumb
= thumb
.convert("RGB")
60 thumb_filepath
= create_pub_filepath(entry
, 'thumbnail.jpg')
62 thumb_file
= mgg
.public_store
.get_file(thumb_filepath
, 'w')
63 with
closing(thumb_file
):
64 thumb
.save(thumb_file
, "JPEG", quality
=90)
67 If the size of the original file exceeds the specified size of a `medium`
68 file, a `medium.jpg` files is created and later associated with the media
71 medium
= Image
.open(queued_filename
)
72 medium_processed
= False
74 if medium
.size
[0] > MEDIUM_SIZE
[0] or medium
.size
[1] > MEDIUM_SIZE
[1]:
75 medium
.thumbnail(MEDIUM_SIZE
, Image
.ANTIALIAS
)
77 if medium
.mode
!= "RGB":
78 medium
= medium
.convert("RGB")
80 medium_filepath
= create_pub_filepath(entry
, 'medium.jpg')
82 medium_file
= mgg
.public_store
.get_file(medium_filepath
, 'w')
83 with
closing(medium_file
):
84 medium
.save(medium_file
, "JPEG", quality
=90)
85 medium_processed
= True
87 # we have to re-read because unlike PIL, not everything reads
88 # things in string representation :)
89 queued_file
= file(queued_filename
, 'rb')
92 original_filepath
= create_pub_filepath(entry
, queued_filepath
[-1])
94 with
closing(mgg
.public_store
.get_file(original_filepath
, 'wb')) as original_file
:
95 original_file
.write(queued_file
.read())
97 mgg
.queue_store
.delete_file(queued_filepath
)
98 entry
['queued_media_file'] = []
99 media_files_dict
= entry
.setdefault('media_files', {})
100 media_files_dict
['thumb'] = thumb_filepath
101 media_files_dict
['original'] = original_filepath
103 media_files_dict
['medium'] = medium_filepath
104 entry
['state'] = u
'processed'
108 workbench
.destroy_self()