Merge remote branch 'origin/master' into bug261-resized-filenames
[mediagoblin.git] / mediagoblin / media_types / ascii / processing.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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 import chardet
17 import os
18 import Image
19 import logging
20
21 from mediagoblin import mg_globals as mgg
22 from mediagoblin.processing import create_pub_filepath, THUMB_SIZE
23 from mediagoblin.media_types.ascii import asciitoimage
24
25 _log = logging.getLogger(__name__)
26
27 def process_ascii(entry):
28 '''
29 Code to process a txt file
30 '''
31 workbench = mgg.workbench_manager.create_workbench()
32 # Conversions subdirectory to avoid collisions
33 conversions_subdir = os.path.join(
34 workbench.dir, 'conversions')
35 os.mkdir(conversions_subdir)
36
37 queued_filepath = entry.queued_media_file
38 queued_filename = workbench.localized_file(
39 mgg.queue_store, queued_filepath,
40 'source')
41
42 queued_file = file(queued_filename, 'rb')
43
44 with queued_file:
45 queued_file_charset = chardet.detect(queued_file.read())
46
47 # Only select a non-utf-8 charset if chardet is *really* sure
48 # Tested with "Feli\x0109an superjaron", which was detecte
49 if queued_file_charset['confidence'] < 0.9:
50 interpreted_charset = 'utf-8'
51 else:
52 interpreted_charset = queued_file_charset['encoding']
53
54 _log.info('Charset detected: {0}\nWill interpret as: {1}'.format(
55 queued_file_charset,
56 interpreted_charset))
57
58 queued_file.seek(0) # Rewind the queued file
59
60 thumb_filepath = create_pub_filepath(
61 entry, 'thumbnail.png')
62
63 tmp_thumb_filename = os.path.join(
64 conversions_subdir, thumb_filepath[-1])
65
66 converter = asciitoimage.AsciiToImage()
67
68 thumb = converter._create_image(
69 queued_file.read())
70
71 with file(tmp_thumb_filename, 'w') as thumb_file:
72 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
73 thumb.save(thumb_file)
74
75 _log.debug('Copying local file to public storage')
76 mgg.public_store.copy_local_to_storage(
77 tmp_thumb_filename, thumb_filepath)
78
79 queued_file.seek(0)
80
81 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
82
83 with mgg.public_store.get_file(original_filepath, 'wb') \
84 as original_file:
85 original_file.write(queued_file.read())
86
87
88 queued_file.seek(0) # Rewind *again*
89
90 unicode_filepath = create_pub_filepath(entry, 'ascii-portable.txt')
91
92 with mgg.public_store.get_file(unicode_filepath, 'wb') \
93 as unicode_file:
94 # Decode the original file from its detected charset (or UTF8)
95 # Encode the unicode instance to ASCII and replace any non-ASCII
96 # with an HTML entity (&#
97 unicode_file.write(
98 unicode(queued_file.read().decode(
99 interpreted_charset)).encode(
100 'ascii',
101 'xmlcharrefreplace'))
102
103 mgg.queue_store.delete_file(queued_filepath)
104 entry.queued_media_file = []
105 media_files_dict = entry.setdefault('media_files', {})
106 media_files_dict['thumb'] = thumb_filepath
107 media_files_dict['unicode'] = unicode_filepath
108 media_files_dict['original'] = original_filepath
109
110 entry.save()