Add priority to the celery tasks
[mediagoblin.git] / mediagoblin / media_types / raw_image / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2014 MediaGoblin contributors. See AUTHORS.
3 #
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.
8 #
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.
13 #
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/>.
16
17 import os
18 import logging
19
20 # This needs to handle the case where it's missing
21 import pyexiv2
22
23 from mediagoblin.media_types.image.processing import (
24 InitialProcessor, Resizer)
25 from mediagoblin.processing import (
26 FilenameBuilder, ProcessingManager)
27
28
29 _log = logging.getLogger(__name__)
30
31 MEDIA_TYPE = 'mediagoblin.media_types.raw_image'
32 ACCEPTED_EXTENSIONS = ['nef', 'cr2']
33
34
35 # The entire function have to be copied
36
37 def sniff_handler(media_file, filename):
38 _log.info('Sniffing {0}'.format(MEDIA_TYPE))
39 name, ext = os.path.splitext(filename)
40 clean_ext = ext[1:].lower() # Strip the . from ext and make lowercase
41
42 if clean_ext in ACCEPTED_EXTENSIONS:
43 _log.info('Found file extension in supported filetypes')
44 return MEDIA_TYPE
45 else:
46 _log.debug('Media present, extension not found in {0}'.format(
47 ACCEPTED_EXTENSIONS))
48
49 return None
50
51
52 class InitialRawProcessor(InitialProcessor):
53 def common_setup(self):
54 """
55 Pull out a full-size JPEG-preview
56 """
57 super(InitialRawProcessor, self).common_setup()
58
59 self._original_raw = self.process_filename
60
61 # Read EXIF data
62 md = pyexiv2.ImageMetadata(self._original_raw)
63 md.read()
64 self.process_filename = os.path.join(self.conversions_subdir,
65 self.entry.queued_media_file[-1])
66
67 # Extract the biggest preview and write it as our working image
68 md.previews[-1].write_to_file(
69 self.process_filename.encode('utf-8'))
70 self.process_filename += '.jpg'
71 _log.debug(u'Wrote new file from {0} to preview (jpg) {1}'.format(
72 self._original_raw, self.process_filename))
73
74 # Override the namebuilder with our new jpg-based name
75 self.name_builder = FilenameBuilder(self.process_filename)
76
77
78 class RawImageProcessingManager(ProcessingManager):
79 def __init__(self):
80 super(RawImageProcessingManager, self).__init__()
81 self.add_processor(InitialRawProcessor)
82 self.add_processor(Resizer)
83
84 def workflow(self, entry, manager, feed_url, reprocess_action,
85 reprocess_info=None):
86 ProcessMedia().apply_async(
87 [entry.id, feed_url, reprocess_action, reprocess_info], {},
88 task_id=entry.queued_task_id)