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/>.
19 from celery
.task
import Task
20 from celery
import registry
22 from mediagoblin
.db
.util
import ObjectId
23 from mediagoblin
import mg_globals
as mgg
25 from mediagoblin
.util
import lazy_pass_to_ugettext
as _
27 from mediagoblin
.process_media
.errors
import *
30 MEDIUM_SIZE
= 640, 640
33 def create_pub_filepath(entry
, filename
):
34 return mgg
.public_store
.get_unique_filepath(
36 unicode(entry
['_id']),
39 ################################
40 # Media processing initial steps
41 ################################
43 class ProcessMedia(Task
):
45 Pass this entry off for processing.
47 def run(self
, media_id
):
49 Pass the media entry off to the appropriate processing function
50 (for now just process_image...)
52 entry
= mgg
.database
.MediaEntry
.one(
53 {'_id': ObjectId(media_id
)})
55 # Try to process, and handle expected errors.
58 except BaseProcessingFail
, exc
:
59 mark_entry_failed(entry
[u
'_id'], exc
)
62 entry
['state'] = u
'processed'
65 def on_failure(self
, exc
, task_id
, args
, kwargs
, einfo
):
67 If the processing failed we should mark that in the database.
69 Assuming that the exception raised is a subclass of BaseProcessingFail,
70 we can use that to get more information about the failure and store that
71 for conveying information to users about the failure, etc.
74 mark_entry_failed(entry_id
, exc
)
77 process_media
= registry
.tasks
[ProcessMedia
.name
]
80 def mark_entry_failed(entry_id
, exc
):
82 Mark a media entry as having failed in its conversion.
84 Uses the exception that was raised to mark more information. If the
85 exception is a derivative of BaseProcessingFail then we can store extra
86 information that can be useful for users telling them why their media failed
90 - entry_id: The id of the media entry
93 # Was this a BaseProcessingFail? In other words, was this a
94 # type of error that we know how to handle?
95 if isinstance(exc
, BaseProcessingFail
):
96 # Looks like yes, so record information about that failure and any
97 # metadata the user might have supplied.
98 mgg
.database
['media_entries'].update(
100 {'$set': {u
'state': u
'failed',
101 u
'fail_error': exc
.exception_path
,
102 u
'fail_metadata': exc
.metadata
}})
104 # Looks like no, so just mark it as failed and don't record a
105 # failure_error (we'll assume it wasn't handled) and don't record
106 # metadata (in fact overwrite it if somehow it had previous info
108 mgg
.database
['media_entries'].update(
110 {'$set': {u
'state': u
'failed',
112 u
'fail_metadata': {}}})
115 def process_image(entry
):
117 Code to process an image
119 workbench
= mgg
.workbench_manager
.create_workbench()
121 queued_filepath
= entry
['queued_media_file']
122 queued_filename
= workbench
.localized_file(
123 mgg
.queue_store
, queued_filepath
,
127 thumb
= Image
.open(queued_filename
)
131 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
132 # ensure color mode is compatible with jpg
133 if thumb
.mode
!= "RGB":
134 thumb
= thumb
.convert("RGB")
136 thumb_filepath
= create_pub_filepath(entry
, 'thumbnail.jpg')
137 thumb_file
= mgg
.public_store
.get_file(thumb_filepath
, 'w')
140 thumb
.save(thumb_file
, "JPEG", quality
=90)
142 # If the size of the original file exceeds the specified size of a `medium`
143 # file, a `medium.jpg` files is created and later associated with the media
145 medium
= Image
.open(queued_filename
)
146 medium_processed
= False
148 if medium
.size
[0] > MEDIUM_SIZE
[0] or medium
.size
[1] > MEDIUM_SIZE
[1]:
149 medium
.thumbnail(MEDIUM_SIZE
, Image
.ANTIALIAS
)
151 if medium
.mode
!= "RGB":
152 medium
= medium
.convert("RGB")
154 medium_filepath
= create_pub_filepath(entry
, 'medium.jpg')
155 medium_file
= mgg
.public_store
.get_file(medium_filepath
, 'w')
158 medium
.save(medium_file
, "JPEG", quality
=90)
159 medium_processed
= True
161 # we have to re-read because unlike PIL, not everything reads
162 # things in string representation :)
163 queued_file
= file(queued_filename
, 'rb')
166 original_filepath
= create_pub_filepath(entry
, queued_filepath
[-1])
168 with mgg
.public_store
.get_file(original_filepath
, 'wb') as original_file
:
169 original_file
.write(queued_file
.read())
171 mgg
.queue_store
.delete_file(queued_filepath
)
172 entry
['queued_media_file'] = []
173 media_files_dict
= entry
.setdefault('media_files', {})
174 media_files_dict
['thumb'] = thumb_filepath
175 media_files_dict
['original'] = original_filepath
177 media_files_dict
['medium'] = medium_filepath
180 workbench
.destroy_self()