Le repo des sources pour le site web des JM2L
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 

541 Zeilen
22 KiB

  1. # -*- coding: utf8 -*-
  2. from pyramid.view import view_config, view_defaults
  3. from pyramid.response import Response
  4. from pyramid.exceptions import NotFound
  5. from pyramid.httpexceptions import HTTPNotFound, HTTPBadRequest
  6. from PIL import Image
  7. import re, os, shutil
  8. from os import path
  9. import mimetypes
  10. import magic
  11. import subprocess
  12. import cStringIO as StringIO
  13. # Database access imports
  14. from .models import User, Place, Tiers, Event, SallePhy
  15. from .blenderthumbnailer import blend_extract_thumb, write_png
  16. CurrentYear = 2015
  17. MIN_FILE_SIZE = 1 # bytes
  18. MAX_FILE_SIZE = 500000000 # bytes
  19. IMAGE_TYPES = re.compile('image/(gif|p?jpeg|(x-)?png)')
  20. ACCEPTED_MIMES = ['application/pdf',
  21. 'application/vnd.oasis.opendocument.text',
  22. 'application/vnd.oasis.opendocument.text-template',
  23. 'application/vnd.oasis.opendocument.graphics',
  24. 'application/vnd.oasis.opendocument.graphics-template',
  25. 'application/vnd.oasis.opendocument.presentation',
  26. 'application/vnd.oasis.opendocument.presentation-template',
  27. 'application/vnd.oasis.opendocument.spreadsheet',
  28. 'application/vnd.oasis.opendocument.spreadsheet-template',
  29. 'image/svg+xml',
  30. 'application/x-blender'
  31. ]
  32. ACCEPT_FILE_TYPES = IMAGE_TYPES
  33. THUMBNAIL_SIZE = 80
  34. EXPIRATION_TIME = 300 # seconds
  35. IMAGEPATH = [ 'images' ]
  36. DOCPATH = [ 'document' ]
  37. THUMBNAILPATH = [ 'images', 'thumbnails' ]
  38. # change the following to POST if DELETE isn't supported by the webserver
  39. DELETEMETHOD="DELETE"
  40. mimetypes.init()
  41. class MediaPath():
  42. def get_list(self, media_table, linked_id, MediaType=None):
  43. filelist = list()
  44. curpath = self.get_mediapath(media_table, linked_id, None)
  45. if not os.path.isdir(curpath):
  46. return list()
  47. for f in os.listdir(curpath):
  48. if os.path.isdir(os.path.join(curpath,f)):
  49. continue
  50. if f.endswith('.type'):
  51. continue
  52. if f:
  53. filename, ext = os.path.splitext( f )
  54. tmpurl = '/image/%s/%d/%s' % (media_table, linked_id, f.replace(" ","%20"))
  55. if MediaType is None:
  56. filelist.append(tmpurl)
  57. elif MediaType=='Image' and ext.lower() in ['.gif','.jpg','.png','.svg','.jpeg']:
  58. filelist.append(tmpurl)
  59. elif MediaType=='Other' and ext.lower() not in ['.gif','.jpg','.png','.svg','.jpeg']:
  60. filelist.append(tmpurl)
  61. return filelist
  62. def get_thumb(self, media_table, linked_id, MediaType=None):
  63. filelist = list()
  64. curpath = self.get_mediapath(media_table, linked_id, None)
  65. curpath = os.path.join( curpath, 'thumbnails')
  66. if not os.path.isdir(curpath):
  67. return list()
  68. for f in os.listdir(curpath):
  69. filename, ext = os.path.splitext( f )
  70. if os.path.isdir(os.path.join(curpath,f)):
  71. continue
  72. if f.endswith('.type'):
  73. continue
  74. if f:
  75. tmpurl = '/image/%s/%d/thumbnails/%s' % (media_table, linked_id, f.replace(" ","%20"))
  76. if MediaType is None:
  77. filelist.append(tmpurl)
  78. elif MediaType=='Image' and len( os.path.splitext(filename)[1] )==0:
  79. filelist.append(tmpurl)
  80. elif MediaType=='Other' and len( os.path.splitext(filename)[1] ):
  81. filelist.append(tmpurl)
  82. return filelist
  83. def get_mediapath(self, media_table, linked_id, name):
  84. linked_id = str(linked_id)
  85. if media_table in ['tiers', 'place', 'salle']:
  86. # Retrieve Slug
  87. if media_table=='tiers':
  88. slug = Tiers.by_id(linked_id).slug
  89. if media_table=='place':
  90. slug = Place.by_id(linked_id).slug
  91. if media_table=='salle':
  92. slug = SallePhy.by_id(linked_id).slug
  93. p = IMAGEPATH + [ media_table, slug ]
  94. elif media_table=='presse':
  95. # Use Year in linked_id
  96. p = IMAGEPATH + [ media_table, linked_id ]
  97. elif media_table=='tasks':
  98. # Use Current Year
  99. p = IMAGEPATH + [ str(CurrentYear), media_table, linked_id ]
  100. elif media_table=='poles':
  101. # Use Current Year
  102. p = IMAGEPATH + [ str(CurrentYear), media_table, linked_id ]
  103. elif media_table in ['RIB', 'Justif']:
  104. slug = User.by_id(linked_id).slug
  105. p = IMAGEPATH + ['users', slug , media_table ]
  106. elif media_table=='users':
  107. user = User.by_id(linked_id)
  108. if not user:
  109. raise HTTPNotFound()
  110. else:
  111. slug = user.slug
  112. p = IMAGEPATH + ['users', slug ]
  113. elif media_table=='badge':
  114. user = User.by_id(linked_id)
  115. if not user:
  116. raise HTTPNotFound()
  117. else:
  118. slug = user.slug
  119. p = IMAGEPATH + ['badge', slug ]
  120. elif media_table=='event':
  121. ev = Event.by_id(linked_id)
  122. slug = ev.slug
  123. year = ev.for_year
  124. p = IMAGEPATH + ['event', str(year), slug ]
  125. if name:
  126. p += [ name ]
  127. TargetPath = os.path.join('jm2l/upload', *p)
  128. if not os.path.isdir(os.path.dirname(TargetPath)):
  129. try:
  130. os.makedirs(os.path.dirname(TargetPath))
  131. except OSError, e:
  132. if e.errno != 17:
  133. raise e
  134. return os.path.join('jm2l/upload', *p)
  135. def ExtMimeIcon(self, mime):
  136. if mime=='application/pdf':
  137. return "/img/PDF.png"
  138. def check_blend_file(self, fileobj):
  139. head = fileobj.read(12)
  140. fileobj.seek(0)
  141. if head[:2] == b'\x1f\x8b': # gzip magic
  142. import zlib
  143. head = zlib.decompress(fileobj.read(), 31)[:12]
  144. fileobj.seek(0)
  145. if head.startswith(b'BLENDER'):
  146. return True
  147. def get_mimetype_from_file(self, fileobj):
  148. mimetype = magic.from_buffer(fileobj.read(1024), mime=True)
  149. fileobj.seek(0)
  150. # Check if the binary file is a blender file
  151. if ( mimetype == "application/octet-stream" or mimetype == "application/x-gzip" ) and self.check_blend_file(fileobj):
  152. return "application/x-blender", True
  153. else:
  154. return mimetype, False
  155. def get_mimetype(self, name):
  156. """ This function return the mime-type based on .type file """
  157. try:
  158. with open(self.mediapath(name) + '.type', 'r', 16) as f:
  159. mime = f.read()
  160. return mime
  161. except IOError:
  162. return None
  163. @view_defaults(route_name='media_upload')
  164. class MediaUpload(MediaPath):
  165. def __init__(self, request):
  166. self.request = request
  167. self.media_table = self.request.matchdict.get('media_table')
  168. self.display_only = False
  169. if self.media_table.startswith('_'):
  170. self.display_only = True
  171. self.media_table = self.media_table[1:]
  172. self.linked_id = self.request.matchdict.get('uid')
  173. if not self.linked_id.isdigit():
  174. raise HTTPBadRequest('Wrong Parameter')
  175. request.response.headers['Access-Control-Allow-Origin'] = '*'
  176. request.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS, HEAD, GET, POST, PUT, DELETE'
  177. def mediapath(self, name):
  178. return self.get_mediapath(self.media_table, self.linked_id, name)
  179. def validate(self, result, filecontent):
  180. """ Do some basic check to the uploaded file before to accept it """
  181. # Try to determine mime type from content uploaded
  182. found_mime = magic.from_buffer(filecontent.read(1024), mime=True)
  183. filecontent.seek(0)
  184. # Do a special statement for specific detected mime type
  185. if found_mime in ["application/octet-stream", "application/x-gzip"]:
  186. # Lets see if it's a bender file
  187. if self.check_blend_file(filecontent):
  188. found_mime = "application/x-blender"
  189. # MonKey Patch of content type
  190. result['type'] = found_mime
  191. # Reject mime type that don't match
  192. if found_mime!=result['type']:
  193. result['error'] = 'L\'extension du fichier ne correspond pas à son contenu - '
  194. result['error'] += "( %s vs %s )" % (found_mime, result['type'])
  195. return False
  196. # Accept images and mime types listed
  197. if not found_mime in ACCEPTED_MIMES:
  198. if not (IMAGE_TYPES.match(found_mime)):
  199. result['error'] = 'Ce type fichier n\'est malheureusement pas supporté. '
  200. return False
  201. if result['size'] < MIN_FILE_SIZE:
  202. result['error'] = 'le fichier est trop petit'
  203. elif result['size'] > MAX_FILE_SIZE:
  204. result['error'] = 'le fichier est trop voluminueux'
  205. #elif not ACCEPT_FILE_TYPES.match(file['type']):
  206. # file['error'] = u'les type de fichiers acceptés sont png, jpg et gif'
  207. else:
  208. return True
  209. return False
  210. def get_file_size(self, fileobj):
  211. fileobj.seek(0, 2) # Seek to the end of the file
  212. size = fileobj.tell() # Get the position of EOF
  213. fileobj.seek(0) # Reset the file position to the beginning
  214. return size
  215. def thumbnailurl(self,name):
  216. return self.request.route_url('media_view',name='thumbnails',
  217. media_table=self.media_table,
  218. uid=self.linked_id) + '/' + name
  219. def thumbnailpath(self,name):
  220. origin = self.mediapath(name)
  221. TargetPath = os.path.join( os.path.dirname(origin), 'thumbnails', name)
  222. if not os.path.isdir(os.path.dirname(TargetPath)):
  223. os.makedirs(os.path.dirname(TargetPath))
  224. return TargetPath
  225. def createthumbnail(self, filename):
  226. image = Image.open( self.mediapath(filename) )
  227. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  228. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  229. timage.paste(
  230. image,
  231. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  232. TargetFileName = self.thumbnailpath(filename)
  233. timage.save( TargetFileName )
  234. return self.thumbnailurl( os.path.basename(TargetFileName) )
  235. def pdfthumbnail(self, filename):
  236. TargetFileName = self.thumbnailpath(filename)
  237. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  238. Result = subprocess.call(Command)
  239. if Result==0:
  240. image = Image.open( TargetFileName+"_.jpg" )
  241. pdf_indicator = Image.open( "jm2l/static/img/PDF_Thumb_Stamp.png" )
  242. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  243. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  244. # Add thumbnail
  245. timage.paste(
  246. image,
  247. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  248. # Stamp with PDF file type
  249. timage.paste(
  250. pdf_indicator,
  251. (timage.size[0]-30, timage.size[1]-30),
  252. pdf_indicator,
  253. )
  254. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  255. os.unlink(TargetFileName+"_.jpg")
  256. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  257. return self.ExtMimeIcon('application/pdf')
  258. def svgthumbnail(self, filename):
  259. TargetFileName = self.thumbnailpath(filename)
  260. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  261. Result = subprocess.call(Command)
  262. if Result==0:
  263. image = Image.open( TargetFileName+"_.jpg" )
  264. pdf_indicator = Image.open( "jm2l/static/img/svg-icon.png" )
  265. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  266. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  267. # Add thumbnail
  268. timage.paste(
  269. image,
  270. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  271. # Stamp with PDF file type
  272. timage.paste(
  273. pdf_indicator,
  274. (timage.size[0]-30, timage.size[1]-30),
  275. pdf_indicator,
  276. )
  277. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  278. os.unlink(TargetFileName+"_.jpg")
  279. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  280. return self.ExtMimeIcon('image/svg+xml')
  281. def docthumbnail(self, filename):
  282. TargetFileName = self.thumbnailpath(filename)
  283. # let's take the thumbnail generated inside the document
  284. Command = ["unzip", "-p", self.mediapath(filename), "Thumbnails/thumbnail.png"]
  285. ThumbBytes = subprocess.check_output(Command)
  286. image = Image.open( StringIO.StringIO(ThumbBytes) )
  287. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  288. # Use the correct stamp
  289. f, ext = os.path.splitext( filename )
  290. istamp = [ ('Writer','odt'),
  291. ('Impress','odp'),
  292. ('Calc','ods'),
  293. ('Draw','odg')]
  294. stampfilename = filter(lambda (x,y): ext.endswith(y), istamp)
  295. stamp = Image.open( "jm2l/static/img/%s-icon.png" % stampfilename[0][0])
  296. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  297. # Add thumbnail
  298. timage.paste(
  299. image,
  300. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  301. # Stamp with PDF file type
  302. timage.paste(
  303. stamp,
  304. (timage.size[0]-30, timage.size[1]-30),
  305. stamp,
  306. )
  307. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  308. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  309. def blendthumbnail(self, filename):
  310. blendfile = self.mediapath(filename)
  311. # Extract Thumb
  312. if 0:
  313. head = fileobj.read(12)
  314. fileobj.seek(0)
  315. if head[:2] == b'\x1f\x8b': # gzip magic
  316. import zlib
  317. head = zlib.decompress(fileobj.read(), 31)[:12]
  318. fileobj.seek(0)
  319. buf, width, height = blend_extract_thumb(blendfile)
  320. if buf:
  321. png = write_png(buf, width, height)
  322. TargetFileName = self.thumbnailpath(filename)
  323. image = Image.open(StringIO.StringIO(png))
  324. blender_indicator = Image.open( "jm2l/static/img/Blender_Thumb_Stamp.png" )
  325. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  326. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  327. # Add thumbnail
  328. timage.paste(
  329. image,
  330. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  331. # Stamp with Blender file type
  332. timage.paste(
  333. blender_indicator,
  334. (timage.size[0]-30, timage.size[1]-30),
  335. blender_indicator,
  336. )
  337. timage.save( TargetFileName+".png")
  338. return self.thumbnailurl( os.path.basename(TargetFileName+".png") )
  339. return self.ExtMimeIcon('application/x-blender')
  340. def fileinfo(self,name):
  341. filename = self.mediapath(name)
  342. f, ext = os.path.splitext(name)
  343. if ext!='.type' and os.path.isfile(filename):
  344. info = {}
  345. info['name'] = name
  346. info['size'] = os.path.getsize(filename)
  347. info['url'] = self.request.route_url('media_view',
  348. name=name,
  349. media_table=self.media_table,
  350. uid=self.linked_id)
  351. mime = self.get_mimetype(name)
  352. if IMAGE_TYPES.match(mime):
  353. info['thumbnailUrl'] = self.thumbnailurl(name)
  354. elif mime in ACCEPTED_MIMES:
  355. thumb = self.thumbnailpath("%s%s" % (f, ext))
  356. thumbext = ".jpg"
  357. if mime == "application/x-blender":
  358. thumbext = ".png"
  359. if os.path.exists( thumb + thumbext ):
  360. info['thumbnailUrl'] = self.thumbnailurl(name)+thumbext
  361. else:
  362. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  363. else:
  364. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  365. if not self.display_only:
  366. info['deleteType'] = DELETEMETHOD
  367. info['deleteUrl'] = self.request.route_url('media_upload',
  368. sep='',
  369. name='',
  370. media_table=self.media_table,
  371. uid=self.linked_id) + '/' + name
  372. if DELETEMETHOD != 'DELETE':
  373. info['deleteUrl'] += '&_method=DELETE'
  374. return info
  375. else:
  376. return None
  377. @view_config(request_method='OPTIONS')
  378. def options(self):
  379. return Response(body='')
  380. @view_config(request_method='HEAD')
  381. def options(self):
  382. return Response(body='')
  383. @view_config(request_method='GET', renderer="json")
  384. def get(self):
  385. p = self.request.matchdict.get('name')
  386. if p:
  387. return self.fileinfo(p)
  388. else:
  389. filelist = []
  390. content = self.mediapath('')
  391. if content and path.exists(content):
  392. for f in os.listdir(content):
  393. n = self.fileinfo(f)
  394. if n:
  395. filelist.append(n)
  396. return { "files":filelist }
  397. @view_config(request_method='DELETE', xhr=True, accept="application/json", renderer='json')
  398. def delete(self):
  399. filename = self.request.matchdict.get('name')
  400. try:
  401. os.remove(self.mediapath(filename) + '.type')
  402. except IOError:
  403. pass
  404. except OSError:
  405. pass
  406. try:
  407. os.remove(self.thumbnailpath(filename))
  408. except IOError:
  409. pass
  410. except OSError:
  411. pass
  412. try:
  413. os.remove(self.thumbnailpath(filename+".jpg"))
  414. except IOError:
  415. pass
  416. except OSError:
  417. pass
  418. try:
  419. os.remove(self.mediapath(filename))
  420. except IOError:
  421. return False
  422. return True
  423. @view_config(request_method='POST', xhr=True, accept="application/json", renderer='json')
  424. def post(self):
  425. if self.request.matchdict.get('_method') == "DELETE":
  426. return self.delete()
  427. results = []
  428. for name, fieldStorage in self.request.POST.items():
  429. if isinstance(fieldStorage,unicode):
  430. continue
  431. result = {}
  432. result['name'] = os.path.basename(fieldStorage.filename)
  433. result['type'] = fieldStorage.type
  434. result['size'] = self.get_file_size(fieldStorage.file)
  435. if self.validate(result, fieldStorage.file):
  436. # Keep mime-type in .type file
  437. with open( self.mediapath( result['name'] ) + '.type', 'w') as f:
  438. f.write(result['type'])
  439. # Store uploaded file
  440. fieldStorage.file.seek(0)
  441. with open( self.mediapath(result['name'] ), 'wb') as f:
  442. shutil.copyfileobj( fieldStorage.file , f)
  443. if re.match(IMAGE_TYPES, result['type']):
  444. result['thumbnailUrl'] = self.createthumbnail(result['name'])
  445. elif result['type']=='application/pdf':
  446. result['thumbnailUrl'] = self.pdfthumbnail(result['name'])
  447. elif result['type']=='image/svg+xml':
  448. result['thumbnailUrl'] = self.svgthumbnail(result['name'])
  449. elif result['type'].startswith('application/vnd'):
  450. result['thumbnailUrl'] = self.docthumbnail(result['name'])
  451. elif result['type']=='application/x-blender':
  452. result['thumbnailUrl'] = self.blendthumbnail(result['name'])
  453. else:
  454. result['thumbnailUrl'] = self.ExtMimeIcon(result['type'])
  455. result['deleteType'] = DELETEMETHOD
  456. result['deleteUrl'] = self.request.route_url('media_upload',
  457. sep='',
  458. name='',
  459. media_table=self.media_table,
  460. uid=self.linked_id) + '/' + result['name']
  461. result['url'] = self.request.route_url('media_view',
  462. media_table=self.media_table,
  463. uid=self.linked_id,
  464. name=result['name'])
  465. if DELETEMETHOD != 'DELETE':
  466. result['deleteUrl'] += '&_method=DELETE'
  467. results.append(result)
  468. return {"files":results}
  469. @view_defaults(route_name='media_view')
  470. class MediaView(MediaPath):
  471. def __init__(self,request):
  472. self.request = request
  473. self.media_table = self.request.matchdict.get('media_table')
  474. self.linked_id = self.request.matchdict.get('uid')
  475. def mediapath(self,name):
  476. return self.get_mediapath(self.media_table, self.linked_id, name)
  477. @view_config(request_method='GET', http_cache = (EXPIRATION_TIME, {'public':True}))
  478. def get(self):
  479. name = self.request.matchdict.get('name')
  480. self.request.response.content_type = self.get_mimetype(name)
  481. try:
  482. self.request.response.body_file = open( self.mediapath(name), 'rb', 10000)
  483. except IOError:
  484. raise NotFound
  485. return self.request.response
  486. ##return Response(app_iter=ImgHandle, content_type = 'image/png')