1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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 werkzeug
.utils
import secure_filename
20 from werkzeug
.datastructures
import FileStorage
22 from mediagoblin
.db
.models
import MediaEntry
23 from mediagoblin
.processing
import mark_entry_failed
24 from mediagoblin
.processing
.task
import ProcessMedia
27 _log
= logging
.getLogger(__name__
)
30 def check_file_field(request
, field_name
):
31 """Check if a file field meets minimal criteria"""
32 retval
= (field_name
in request
.files
33 and isinstance(request
.files
[field_name
], FileStorage
)
34 and request
.files
[field_name
].stream
)
36 _log
.debug("Form did not contain proper file field %s", field_name
)
40 def new_upload_entry(user
):
42 Create a new MediaEntry for uploading
45 entry
.uploader
= user
.id
46 entry
.license
= user
.license_preference
50 def prepare_queue_task(app
, entry
, filename
):
52 Prepare a MediaEntry for the processing queue and get a queue file
54 # We generate this ourselves so we know what the task id is for
57 # (If we got it off the task's auto-generation, there'd be
58 # a risk of a race condition when we'd save after sending
60 task_id
= unicode(uuid
.uuid4())
61 entry
.queued_task_id
= task_id
63 # Now store generate the queueing related filename
64 queue_filepath
= app
.queue_store
.get_unique_filepath(
67 secure_filename(filename
)])
70 queue_file
= app
.queue_store
.get_file(
73 # Add queued filename to the entry
74 entry
.queued_media_file
= queue_filepath
79 def run_process_media(entry
, feed_url
=None,
80 reprocess_action
="initial", reprocess_info
=None):
81 """Process the media asynchronously
83 :param entry: MediaEntry() instance to be processed.
84 :param feed_url: A string indicating the feed_url that the PuSH servers
85 should be notified of. This will be sth like: `request.urlgen(
86 'mediagoblin.user_pages.atom_feed',qualified=True,
87 user=request.user.username)`
88 :param reprocess_action: What particular action should be run.
89 :param reprocess_info: A dict containing all of the necessary reprocessing
90 info for the given media_type"""
92 ProcessMedia().apply_async(
93 [entry
.id, feed_url
, reprocess_action
, reprocess_info
], {},
94 task_id
=entry
.queued_task_id
)
95 except BaseException
as exc
:
96 # The purpose of this section is because when running in "lazy"
97 # or always-eager-with-exceptions-propagated celery mode that
98 # the failure handling won't happen on Celery end. Since we
99 # expect a lot of users to run things in this way we have to
100 # capture stuff here.
102 # ... not completely the diaper pattern because the
103 # exception is re-raised :)
104 mark_entry_failed(entry
.id, exc
)
105 # re-raise the exception