Commit | Line | Data |
---|---|---|
a246ccca | 1 | # GNU MediaGoblin -- federated, autonomous media hosting |
cf29e8a8 | 2 | # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. |
a246ccca JW |
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 | import asciitoimage | |
17 | import chardet | |
18 | import os | |
19 | import Image | |
20 | ||
21 | from mediagoblin import mg_globals as mgg | |
22 | from mediagoblin.processing import create_pub_filepath, THUMB_SIZE | |
23 | ||
24 | ||
25 | def process_ascii(entry): | |
26 | ''' | |
27 | Code to process a txt file | |
28 | ''' | |
29 | workbench = mgg.workbench_manager.create_workbench() | |
30 | # Conversions subdirectory to avoid collisions | |
31 | conversions_subdir = os.path.join( | |
32 | workbench.dir, 'conversions') | |
33 | os.mkdir(conversions_subdir) | |
34 | ||
35 | queued_filepath = entry['queued_media_file'] | |
36 | queued_filename = workbench.localized_file( | |
37 | mgg.queue_store, queued_filepath, | |
38 | 'source') | |
39 | ||
40 | queued_file = file(queued_filename, 'rb') | |
41 | ||
42 | with queued_file: | |
43 | queued_file_charset = chardet.detect(queued_file.read()) | |
44 | ||
45 | queued_file.seek(0) # Rewind the queued file | |
46 | ||
47 | thumb_filepath = create_pub_filepath( | |
48 | entry, 'thumbnail.png') | |
49 | ||
50 | tmp_thumb_filename = os.path.join( | |
51 | conversions_subdir, thumb_filepath[-1]) | |
52 | ||
53 | converter = asciitoimage.AsciiToImage() | |
54 | ||
55 | thumb = converter._create_image( | |
56 | queued_file.read()) | |
57 | ||
58 | with file(tmp_thumb_filename, 'w') as thumb_file: | |
59 | thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS) | |
60 | thumb.save(thumb_file) | |
61 | ||
62 | mgg.public_store.copy_local_to_storage( | |
63 | tmp_thumb_filename, thumb_filepath) | |
64 | ||
65 | queued_file.seek(0) | |
66 | ||
67 | original_filepath = create_pub_filepath(entry, queued_filepath[-1]) | |
68 | ||
69 | with mgg.public_store.get_file(original_filepath, 'wb') \ | |
70 | as original_file: | |
71 | original_file.write(queued_file.read()) | |
72 | ||
73 | ||
74 | queued_file.seek(0) # Rewind *again* | |
75 | ||
76 | unicode_filepath = create_pub_filepath(entry, 'unicode.txt') | |
77 | ||
78 | with mgg.public_store.get_file(unicode_filepath, 'wb') \ | |
79 | as unicode_file: | |
80 | unicode_file.write( | |
81 | unicode(queued_file.read().decode( | |
82 | queued_file_charset['encoding'])).encode( | |
83 | 'ascii', | |
84 | 'xmlcharrefreplace')) | |
85 | ||
86 | mgg.queue_store.delete_file(queued_filepath) | |
87 | entry['queued_media_file'] = [] | |
88 | media_files_dict = entry.setdefault('media_files', {}) | |
89 | media_files_dict['thumb'] = thumb_filepath | |
90 | media_files_dict['unicode'] = unicode_filepath | |
91 | media_files_dict['original'] = original_filepath | |
92 | ||
93 | entry.save() |