1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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/>.
20 from celery
.task
import Task
21 from celery
import registry
23 from mediagoblin
.db
.util
import ObjectId
24 from mediagoblin
import mg_globals
as mgg
26 from mediagoblin
.processing
import BaseProcessingFail
, \
27 mark_entry_failed
, BadMediaFail
, create_pub_filepath
, THUMB_SIZE
, \
30 ################################
31 # Media processing initial steps
32 ################################
35 def process_image(entry
):
37 Code to process an image
39 workbench
= mgg
.workbench_manager
.create_workbench()
40 # Conversions subdirectory to avoid collisions
41 conversions_subdir
= os
.path
.join(
42 workbench
.dir, 'conversions')
43 os
.mkdir(conversions_subdir
)
45 queued_filepath
= entry
['queued_media_file']
46 queued_filename
= workbench
.localized_file(
47 mgg
.queue_store
, queued_filepath
,
50 filename_bits
= os
.path
.splitext(queued_filename
)
51 basename
= os
.path
.split(filename_bits
[0])[1]
52 extension
= filename_bits
[1].lower()
55 thumb
= Image
.open(queued_filename
)
59 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
61 # Copy the thumb to the conversion subdir, then remotely.
62 thumb_filename
= 'thumbnail' + extension
63 thumb_filepath
= create_pub_filepath(entry
, thumb_filename
)
64 tmp_thumb_filename
= os
.path
.join(
65 conversions_subdir
, thumb_filename
)
66 with
file(tmp_thumb_filename
, 'w') as thumb_file
:
67 thumb
.save(thumb_file
)
68 mgg
.public_store
.copy_local_to_storage(
69 tmp_thumb_filename
, thumb_filepath
)
71 # If the size of the original file exceeds the specified size of a `medium`
72 # file, a `medium.jpg` files is created and later associated with the media
74 medium
= Image
.open(queued_filename
)
75 medium_processed
= False
77 if medium
.size
[0] > MEDIUM_SIZE
[0] or medium
.size
[1] > MEDIUM_SIZE
[1]:
78 medium
.thumbnail(MEDIUM_SIZE
, Image
.ANTIALIAS
)
80 medium_filename
= 'medium' + extension
81 medium_filepath
= create_pub_filepath(entry
, medium_filename
)
82 tmp_medium_filename
= os
.path
.join(
83 conversions_subdir
, medium_filename
)
85 with
file(tmp_medium_filename
, 'w') as medium_file
:
86 medium
.save(medium_file
)
88 mgg
.public_store
.copy_local_to_storage(
89 tmp_medium_filename
, medium_filepath
)
91 medium_processed
= True
93 # we have to re-read because unlike PIL, not everything reads
94 # things in string representation :)
95 queued_file
= file(queued_filename
, 'rb')
98 #create_pub_filepath(entry, queued_filepath[-1])
99 original_filepath
= create_pub_filepath(entry
, basename
+ extension
)
101 with mgg
.public_store
.get_file(original_filepath
, 'wb') \
103 original_file
.write(queued_file
.read())
105 mgg
.queue_store
.delete_file(queued_filepath
)
106 entry
['queued_media_file'] = []
107 media_files_dict
= entry
.setdefault('media_files', {})
108 media_files_dict
['thumb'] = thumb_filepath
109 media_files_dict
['original'] = original_filepath
111 media_files_dict
['medium'] = medium_filepath
114 workbench
.destroy_self()