Audio thumbnailing & spectrograms, media plugins use sniffing
[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 SUPPORTED_EXTENSIONS = ['txt', 'asc', 'nfo']
28
29 def sniff_handler(media_file, **kw):
30 if not kw.get('media') == None:
31 name, ext = os.path.splitext(kw['media'].filename)
32 clean_ext = ext[1:].lower()
33
34 if clean_ext in SUPPORTED_EXTENSIONS:
35 return True
36
37 return False
38
39 def process_ascii(entry):
40 '''
41 Code to process a txt file
42 '''
43 workbench = mgg.workbench_manager.create_workbench()
44 # Conversions subdirectory to avoid collisions
45 conversions_subdir = os.path.join(
46 workbench.dir, 'conversions')
47 os.mkdir(conversions_subdir)
48
49 queued_filepath = entry['queued_media_file']
50 queued_filename = workbench.localized_file(
51 mgg.queue_store, queued_filepath,
52 'source')
53
54 queued_file = file(queued_filename, 'rb')
55
56 with queued_file:
57 queued_file_charset = chardet.detect(queued_file.read())
58
59 # Only select a non-utf-8 charset if chardet is *really* sure
60 # Tested with "Feli\x0109an superjaron", which was detecte
61 if queued_file_charset['confidence'] < 0.9:
62 interpreted_charset = 'utf-8'
63 else:
64 interpreted_charset = queued_file_charset['encoding']
65
66 _log.info('Charset detected: {0}\nWill interpret as: {1}'.format(
67 queued_file_charset,
68 interpreted_charset))
69
70 queued_file.seek(0) # Rewind the queued file
71
72 thumb_filepath = create_pub_filepath(
73 entry, 'thumbnail.png')
74
75 tmp_thumb_filename = os.path.join(
76 conversions_subdir, thumb_filepath[-1])
77
78 converter = asciitoimage.AsciiToImage()
79
80 thumb = converter._create_image(
81 queued_file.read())
82
83 with file(tmp_thumb_filename, 'w') as thumb_file:
84 thumb.thumbnail(THUMB_SIZE, Image.ANTIALIAS)
85 thumb.save(thumb_file)
86
87 _log.debug('Copying local file to public storage')
88 mgg.public_store.copy_local_to_storage(
89 tmp_thumb_filename, thumb_filepath)
90
91 queued_file.seek(0)
92
93 original_filepath = create_pub_filepath(entry, queued_filepath[-1])
94
95 with mgg.public_store.get_file(original_filepath, 'wb') \
96 as original_file:
97 original_file.write(queued_file.read())
98
99
100 queued_file.seek(0) # Rewind *again*
101
102 unicode_filepath = create_pub_filepath(entry, 'ascii-portable.txt')
103
104 with mgg.public_store.get_file(unicode_filepath, 'wb') \
105 as unicode_file:
106 # Decode the original file from its detected charset (or UTF8)
107 # Encode the unicode instance to ASCII and replace any non-ASCII
108 # with an HTML entity (&#
109 unicode_file.write(
110 unicode(queued_file.read().decode(
111 interpreted_charset)).encode(
112 'ascii',
113 'xmlcharrefreplace'))
114
115 mgg.queue_store.delete_file(queued_filepath)
116 entry['queued_media_file'] = []
117 media_files_dict = entry.setdefault('media_files', {})
118 media_files_dict['thumb'] = thumb_filepath
119 media_files_dict['unicode'] = unicode_filepath
120 media_files_dict['original'] = original_filepath
121
122 entry.save()