1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
25 _log
= logging
.getLogger(__name__
)
27 def process_ascii(entry
):
29 Code to process a txt file
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
)
37 queued_filepath
= entry
.queued_media_file
38 queued_filename
= workbench
.localized_file(
39 mgg
.queue_store
, queued_filepath
,
42 queued_file
= file(queued_filename
, 'rb')
45 queued_file_charset
= chardet
.detect(queued_file
.read())
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'
52 interpreted_charset
= queued_file_charset
['encoding']
54 _log
.info('Charset detected: {0}\nWill interpret as: {1}'.format(
58 queued_file
.seek(0) # Rewind the queued file
60 thumb_filepath
= create_pub_filepath(
61 entry
, 'thumbnail.png')
63 tmp_thumb_filename
= os
.path
.join(
64 conversions_subdir
, thumb_filepath
[-1])
66 converter
= asciitoimage
.AsciiToImage()
68 thumb
= converter
._create
_image
(
71 with
file(tmp_thumb_filename
, 'w') as thumb_file
:
72 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
73 thumb
.save(thumb_file
)
75 _log
.debug('Copying local file to public storage')
76 mgg
.public_store
.copy_local_to_storage(
77 tmp_thumb_filename
, thumb_filepath
)
81 original_filepath
= create_pub_filepath(entry
, queued_filepath
[-1])
83 with mgg
.public_store
.get_file(original_filepath
, 'wb') \
85 original_file
.write(queued_file
.read())
88 queued_file
.seek(0) # Rewind *again*
90 unicode_filepath
= create_pub_filepath(entry
, 'ascii-portable.txt')
92 with mgg
.public_store
.get_file(unicode_filepath
, 'wb') \
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 (&#
98 unicode(queued_file
.read().decode(
99 interpreted_charset
)).encode(
101 'xmlcharrefreplace'))
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