Merge remote branch 'remotes/nyergler/issue-680-csrf-optout'
[mediagoblin.git] / mediagoblin / media_types / image / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 Image
18 import os
19
20 from celery.task import Task
21 from celery import registry
22
23 from mediagoblin.db.util import ObjectId
24 from mediagoblin import mg_globals as mgg
25
26 from mediagoblin.processing import BaseProcessingFail, \
27 mark_entry_failed, BadMediaFail, create_pub_filepath, THUMB_SIZE, \
28 MEDIUM_SIZE
29
30 ################################
31 # Media processing initial steps
32 ################################
33
34
35 def process_image(entry):
36 """
37 Code to process an image
38 """
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)
44
45 queued_filepath = entry['queued_media_file']
46 queued_filename = workbench.localized_file(
47 mgg.queue_store, queued_filepath,
48 'source')
49
50 filename_bits = os.path.splitext(queued_filename)
51 basename = os.path.split(filename_bits[0])[1]
52 extension = filename_bits[1].lower()
53
54 try:
55 thumb = Image.open(queued_filename)
56 except IOError:
57 raise BadMediaFail()
58
59 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
60
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)
70
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
73 # entry.
74 medium = Image.open(queued_filename)
75 medium_processed = False
76
77 if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]:
78 medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS)
79
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)
84
85 with file(tmp_medium_filename, 'w') as medium_file:
86 medium.save(medium_file)
87
88 mgg.public_store.copy_local_to_storage(
89 tmp_medium_filename, medium_filepath)
90
91 medium_processed = True
92
93 # we have to re-read because unlike PIL, not everything reads
94 # things in string representation :)
95 queued_file = file(queued_filename, 'rb')
96
97 with queued_file:
98 #create_pub_filepath(entry, queued_filepath[-1])
99 original_filepath = create_pub_filepath(entry, basename + extension)
100
101 with mgg.public_store.get_file(original_filepath, 'wb') \
102 as original_file:
103 original_file.write(queued_file.read())
104
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
110 if medium_processed:
111 media_files_dict['medium'] = medium_filepath
112
113 # clean up workbench
114 workbench.destroy_self()