forked from hforge/ikaaro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile.py
413 lines (316 loc) · 12.4 KB
/
file.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
# -*- coding: UTF-8 -*-
# Copyright (C) 2005-2008 Juan David Ibáñez Palomar <[email protected]>
# Copyright (C) 2006-2008 Hervé Cauwelier <[email protected]>
# Copyright (C) 2007 Sylvain Taverne <[email protected]>
# Copyright (C) 2007-2008 Henry Obein <[email protected]>
# Copyright (C) 2008 Nicolas Deram <[email protected]>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
# Import from itools
from itools.core import guess_all_extensions
from itools.gettext import MSG
from itools.handlers import File as FileHandler
from itools.handlers import Image as ImageHandler, SVGFile
from itools.handlers import TARFile, ZIPFile, GzipFile, Bzip2File
from itools.odf import SXWFile, SXCFile, SXIFile, ODTFile, ODSFile, ODPFile
from itools.pdf import PDFFile
from itools.office import RTF as RTFFile
from itools.office import MSWord as MSWordFile, MSExcel as MSExcelFile
from itools.office import MSPowerPoint as MSPowerPointFile
from itools.office import MSWordX as MSWordXFile, MSExcelX as MSExcelXFile
from itools.office import MSPowerPointX as MSPowerPointXFile
from itools.web import get_context
# Import from ikaaro
from content import Content
from database import Database
from fields import Char_Field, File_Field, Owner_Field
from file_views import File_NewInstance, File_View
from file_views import File_Edit, File_ExternalEdit, File_ExternalEdit_View
from file_views import File_Download
from file_views import Image_View, Video_View, Archive_View
from file_views import Flash_View
from resource_views import DBResource_GetImage
###########################################################################
# Base File
###########################################################################
class File(Content):
class_id = 'file'
class_version = '20090122'
class_title = MSG(u'File')
class_description = MSG(
u'Upload office documents, images, media files, etc.')
class_icon16 = 'icons/16x16/file.png'
class_icon48 = 'icons/48x48/file.png'
class_views = ['view', 'edit', 'externaledit', 'remove', 'subscribe',
'links', 'backlinks', 'commit_log']
# Fields
data = File_Field(required=True, class_handler=FileHandler)
filename = Char_Field
owner = Owner_Field
def get_all_extensions(self):
format = self.metadata.format
# FIXME This is a hack, compression encodings are not yet properly
# supported (to do for the next major version).
if format == 'application/x-gzip':
extensions = ['gz', 'tgz']
elif format == 'application/x-bzip2':
extensions = ['bz2', 'tbz2']
else:
cls = self.data.class_handler
extensions = [ x[1:] for x in guess_all_extensions(format) ]
if cls.class_extension in extensions:
extensions.remove(cls.class_extension)
extensions.insert(0, cls.class_extension)
return extensions
#######################################################################
# Versioning & Indexing
#######################################################################
def to_text(self):
return self.get_value('data').to_text()
def get_files_to_archive(self, content=False):
# Handlers
files = [ x.key for x in self.get_handlers() ]
# Metadata
metadata = self.metadata.key
files.append(metadata)
return files
#######################################################################
# User Interface
#######################################################################
def get_content_type(self):
return self.get_value('data').get_mimetype()
# Views
new_instance = File_NewInstance
download = File_Download
view = File_View
edit = File_Edit
externaledit = File_ExternalEdit_View
external_edit = File_ExternalEdit
###########################################################################
# Media
###########################################################################
class Image(File):
class_id = 'image'
class_title = MSG(u'Image')
class_icon16 = 'icons/16x16/image.png'
class_icon48 = 'icons/48x48/image.png'
class_views = ['view', 'download', 'edit', 'externaledit', 'remove',
'links', 'backlinks', 'commit_log']
# Fields
data = File.data(class_handler=ImageHandler)
def get_max_width(self):
# Auto-reduce width on init
server = get_context().server
if server is not None:
return server.config.get_value('max-width')
return None
def get_max_height(self):
# Auto-reduce height on init
server = get_context().server
if server is not None:
return server.config.get_value('max-height')
return None
def init_resource(self, **kw):
super(Image, self).init_resource(**kw)
# Resize image at max size
max_width = self.get_max_width()
max_height = self.get_max_height()
if max_width or max_height:
handler = self.get_value('data')
xsize, ysize = handler.get_size()
thumb, format = handler.get_thumbnail(
min(xsize, max_width or xsize),
min(ysize, max_height or ysize))
handler.load_state_from_string(thumb)
# Views
thumb = DBResource_GetImage(field_name='data')
view = Image_View
class SVG(Image):
class_id = 'image/svg+xml'
class_title = MSG(u'Image SVG')
# Fields
data = Image.data(class_handler=SVGFile)
class Video(File):
class_id = 'video'
class_title = MSG(u'Video')
class_description = MSG(u'Video')
class_icon16 = 'icons/16x16/flash.png'
class_icon48 = 'icons/48x48/flash.png'
# Views
view = Video_View
class Flash(File):
class_id = 'application/x-shockwave-flash'
class_title = MSG(u'Flash')
class_description = MSG(u'Flash Document')
class_icon16 = 'icons/16x16/flash.png'
class_icon48 = 'icons/48x48/flash.png'
# Views
view = Flash_View
###########################################################################
# Office Documents
###########################################################################
class MSWord(File):
class_id = 'application/msword'
class_title = MSG(u'Word')
class_description = MSG(u'Word Text')
class_icon16 = 'icons/16x16/word.png'
class_icon48 = 'icons/48x48/word.png'
# Fields
data = File.data(class_handler=MSWordFile)
class MSExcel(File):
class_id = 'application/vnd.ms-excel'
class_title = MSG(u'Excel')
class_description = MSG(u'Excel Spreadsheet')
class_icon16 = 'icons/16x16/excel.png'
class_icon48 = 'icons/48x48/excel.png'
# Fields
data = File.data(class_handler=MSExcelFile)
class MSPowerPoint(File):
class_id = 'application/vnd.ms-powerpoint'
class_title = MSG(u'PowerPoint')
class_description = MSG(u'PowerPoint Presentation')
class_icon16 = 'icons/16x16/powerpoint.png'
class_icon48 = 'icons/48x48/powerpoint.png'
# Fields
data = File.data(class_handler=MSPowerPointFile)
class MSWordX(File):
class_id = MSWordXFile.class_mimetypes[0]
class_title = MSG(u'Word')
class_description = MSG(u'Word Text')
class_icon16 = 'icons/16x16/word.png'
class_icon48 = 'icons/48x48/word.png'
# Fields
data = File.data(class_handler=MSWordXFile)
class MSExcelX(File):
class_id = MSExcelXFile.class_mimetypes[0]
class_title = MSG(u'Excel')
class_description = MSG(u'Excel Spreadsheet')
class_icon16 = 'icons/16x16/excel.png'
class_icon48 = 'icons/48x48/excel.png'
# Fields
data = File.data(class_handler=MSExcelXFile)
class MSPowerPointX(File):
class_id = MSPowerPointXFile.class_mimetypes[0]
class_title = MSG(u'PowerPoint')
class_description = MSG(u'PowerPoint Presentation')
class_icon16 = 'icons/16x16/powerpoint.png'
class_icon48 = 'icons/48x48/powerpoint.png'
# Fields
data = File.data(class_handler=MSPowerPointXFile)
class OOWriter(File):
class_id = 'application/vnd.sun.xml.writer'
class_title = MSG(u'OOo Writer')
class_description = MSG(u'OpenOffice.org Text')
class_icon16 = 'icons/16x16/oowriter.png'
class_icon48 = 'icons/48x48/oowriter.png'
# Fields
data = File.data(class_handler=SXWFile)
class OOCalc(File):
class_id = 'application/vnd.sun.xml.calc'
class_title = MSG(u'OOo Calc')
class_description = MSG(u'OpenOffice.org Spreadsheet')
class_icon16 = 'icons/16x16/oocalc.png'
class_icon48 = 'icons/48x48/oocalc.png'
# Fields
data = File.data(class_handler=SXCFile)
class OOImpress(File):
class_id = 'application/vnd.sun.xml.impress'
class_title = MSG(u'OOo Impress')
class_description = MSG(u'OpenOffice.org Presentation')
class_icon16 = 'icons/16x16/ooimpress.png'
class_icon48 = 'icons/48x48/ooimpress.png'
# Fields
data = File.data(class_handler=SXIFile)
class PDF(File):
class_id = 'application/pdf'
class_title = MSG(u'PDF')
class_description = MSG(u'PDF Document')
class_icon16 = 'icons/16x16/pdf.png'
class_icon48 = 'icons/48x48/pdf.png'
# Fields
data = File.data(class_handler=PDFFile)
class RTF(File):
class_id = 'text/rtf'
class_title = MSG(u"RTF")
class_description = MSG(u'RTF Document')
class_icon16 = 'icons/16x16/text.png'
class_icon48 = 'icons/48x48/text.png'
# Fields
data = File.data(class_handler=RTFFile)
class ODT(File):
class_id = 'application/vnd.oasis.opendocument.text'
class_title = MSG(u'ODT')
class_description = MSG(u'OpenDocument Text')
class_icon16 = 'icons/16x16/odt.png'
class_icon48 = 'icons/48x48/odt.png'
# Fields
data = File.data(class_handler=ODTFile)
class ODS(File):
class_id = 'application/vnd.oasis.opendocument.spreadsheet'
class_title = MSG(u'ODS')
class_description = MSG(u'OpenDocument Spreadsheet')
class_icon16 = 'icons/16x16/ods.png'
class_icon48 = 'icons/48x48/ods.png'
# Fields
data = File.data(class_handler=ODSFile)
class ODP(File):
class_id = 'application/vnd.oasis.opendocument.presentation'
class_title = MSG(u'ODP')
class_description = MSG(u'OpenDocument Presentation')
class_icon16 = 'icons/16x16/odp.png'
class_icon48 = 'icons/48x48/odp.png'
# Fields
data = File.data(class_handler=ODPFile)
###########################################################################
# Archive Files
###########################################################################
class Archive(File):
view = Archive_View
class ZipArchive(Archive):
class_id = 'application/zip'
class_title = MSG(u"Zip")
class_description = MSG(u'Zip Archive')
class_icon16 = 'icons/16x16/zip.png'
class_icon48 = 'icons/48x48/zip.png'
# Fields
data = Archive.data(class_handler=ZIPFile)
class TarArchive(Archive):
class_id = 'application/x-tar'
class_title = MSG(u"Tar")
class_description = MSG(u'Tar Archive')
class_icon16 = 'icons/16x16/tar.png'
class_icon48 = 'icons/48x48/tar.png'
# Fields
data = Archive.data(class_handler=TARFile)
class Gzip(File):
class_id = 'application/x-gzip'
class_title = MSG(u"Gzip")
class_description = MSG(u'Gzip Compressed')
class_icon16 = 'icons/16x16/gzip.png'
class_icon48 = 'icons/48x48/gzip.png'
# Fields
data = File.data(class_handler=GzipFile)
class Bzip2(File):
class_id = 'application/x-bzip2'
class_title = MSG(u"Bzip2")
class_description = MSG(u'Bzip2 Compressed')
class_icon16 = 'icons/16x16/bzip.png'
class_icon48 = 'icons/48x48/bzip.png'
# Fields
data = File.data(class_handler=Bzip2File)
###########################################################################
# Register
###########################################################################
Database.register_resource_class(File, 'application/octet-stream')
Database.register_resource_class(ZipArchive, 'application/x-zip-compressed')