Commit | Line | Data |
---|---|---|
93bdab9d JW |
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 | |
8e5f9746 | 18 | import os |
93bdab9d | 19 | |
93bdab9d JW |
20 | from mediagoblin import mg_globals as mgg |
21 | ||
4535f759 JW |
22 | from mediagoblin.processing import BadMediaFail, \ |
23 | create_pub_filepath, THUMB_SIZE, MEDIUM_SIZE | |
93bdab9d | 24 | |
93bdab9d JW |
25 | ################################ |
26 | # Media processing initial steps | |
27 | ################################ | |
28 | ||
93bdab9d | 29 | |
93bdab9d JW |
30 | def process_image(entry): |
31 | """ | |
32 | Code to process an image | |
33 | """ | |
34 | workbench = mgg.workbench_manager.create_workbench() | |
8e5f9746 JW |
35 | # Conversions subdirectory to avoid collisions |
36 | conversions_subdir = os.path.join( | |
37 | workbench.dir, 'conversions') | |
38 | os.mkdir(conversions_subdir) | |
93bdab9d JW |
39 | |
40 | queued_filepath = entry['queued_media_file'] | |
41 | queued_filename = workbench.localized_file( | |
42 | mgg.queue_store, queued_filepath, | |
43 | 'source') | |
44 | ||
58dd8d9e PUS |
45 | filename_bits = os.path.splitext(queued_filename) |
46 | basename = os.path.split(filename_bits[0])[1] | |
47 | extension = filename_bits[1].lower() | |
8e5f9746 | 48 | |
93bdab9d JW |
49 | try: |
50 | thumb = Image.open(queued_filename) | |
51 | except IOError: | |
52 | raise BadMediaFail() | |
53 | ||
54 | thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) | |
93bdab9d | 55 | |
8e5f9746 JW |
56 | # Copy the thumb to the conversion subdir, then remotely. |
57 | thumb_filename = 'thumbnail' + extension | |
58 | thumb_filepath = create_pub_filepath(entry, thumb_filename) | |
59 | tmp_thumb_filename = os.path.join( | |
60 | conversions_subdir, thumb_filename) | |
61 | with file(tmp_thumb_filename, 'w') as thumb_file: | |
62 | thumb.save(thumb_file) | |
63 | mgg.public_store.copy_local_to_storage( | |
64 | tmp_thumb_filename, thumb_filepath) | |
93bdab9d JW |
65 | |
66 | # If the size of the original file exceeds the specified size of a `medium` | |
67 | # file, a `medium.jpg` files is created and later associated with the media | |
68 | # entry. | |
69 | medium = Image.open(queued_filename) | |
70 | medium_processed = False | |
71 | ||
72 | if medium.size[0] > MEDIUM_SIZE[0] or medium.size[1] > MEDIUM_SIZE[1]: | |
73 | medium.thumbnail(MEDIUM_SIZE, Image.ANTIALIAS) | |
74 | ||
8e5f9746 JW |
75 | medium_filename = 'medium' + extension |
76 | medium_filepath = create_pub_filepath(entry, medium_filename) | |
77 | tmp_medium_filename = os.path.join( | |
78 | conversions_subdir, medium_filename) | |
79 | ||
80 | with file(tmp_medium_filename, 'w') as medium_file: | |
81 | medium.save(medium_file) | |
93bdab9d | 82 | |
8e5f9746 JW |
83 | mgg.public_store.copy_local_to_storage( |
84 | tmp_medium_filename, medium_filepath) | |
93bdab9d | 85 | |
8e5f9746 | 86 | medium_processed = True |
93bdab9d JW |
87 | |
88 | # we have to re-read because unlike PIL, not everything reads | |
89 | # things in string representation :) | |
90 | queued_file = file(queued_filename, 'rb') | |
91 | ||
92 | with queued_file: | |
58dd8d9e PUS |
93 | #create_pub_filepath(entry, queued_filepath[-1]) |
94 | original_filepath = create_pub_filepath(entry, basename + extension) | |
93bdab9d | 95 | |
8e5f9746 JW |
96 | with mgg.public_store.get_file(original_filepath, 'wb') \ |
97 | as original_file: | |
93bdab9d JW |
98 | original_file.write(queued_file.read()) |
99 | ||
100 | mgg.queue_store.delete_file(queued_filepath) | |
101 | entry['queued_media_file'] = [] | |
102 | media_files_dict = entry.setdefault('media_files', {}) | |
103 | media_files_dict['thumb'] = thumb_filepath | |
104 | media_files_dict['original'] = original_filepath | |
105 | if medium_processed: | |
106 | media_files_dict['medium'] = medium_filepath | |
107 | ||
108 | # clean up workbench | |
109 | workbench.destroy_self() |