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
25 def process_ascii(entry
):
27 Code to process a txt file
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
)
35 queued_filepath
= entry
['queued_media_file']
36 queued_filename
= workbench
.localized_file(
37 mgg
.queue_store
, queued_filepath
,
40 queued_file
= file(queued_filename
, 'rb')
43 queued_file_charset
= chardet
.detect(queued_file
.read())
45 queued_file
.seek(0) # Rewind the queued file
47 thumb_filepath
= create_pub_filepath(
48 entry
, 'thumbnail.png')
50 tmp_thumb_filename
= os
.path
.join(
51 conversions_subdir
, thumb_filepath
[-1])
53 converter
= asciitoimage
.AsciiToImage()
55 thumb
= converter
._create
_image
(
58 with
file(tmp_thumb_filename
, 'w') as thumb_file
:
59 thumb
.thumbnail(THUMB_SIZE
, Image
.ANTIALIAS
)
60 thumb
.save(thumb_file
)
62 mgg
.public_store
.copy_local_to_storage(
63 tmp_thumb_filename
, thumb_filepath
)
67 original_filepath
= create_pub_filepath(entry
, queued_filepath
[-1])
69 with mgg
.public_store
.get_file(original_filepath
, 'wb') \
71 original_file
.write(queued_file
.read())
74 queued_file
.seek(0) # Rewind *again*
76 unicode_filepath
= create_pub_filepath(entry
, 'unicode.txt')
78 with mgg
.public_store
.get_file(unicode_filepath
, 'wb') \
81 unicode(queued_file
.read().decode(
82 queued_file_charset
['encoding'])).encode(
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