Le repo des sources pour le site web des JM2L
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

532 lines
21 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 ] + [ self.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. os.makedirs(os.path.dirname(TargetPath))
  130. return os.path.join('jm2l/upload', *p)
  131. def ExtMimeIcon(self, mime):
  132. if mime=='application/pdf':
  133. return "/img/PDF.png"
  134. def check_blend_file(self, fileobj):
  135. head = fileobj.read(12)
  136. fileobj.seek(0)
  137. if head[:2] == b'\x1f\x8b': # gzip magic
  138. import zlib
  139. head = zlib.decompress(fileobj.read(), 31)[:12]
  140. fileobj.seek(0)
  141. if head.startswith(b'BLENDER'):
  142. return True
  143. def get_mimetype_from_file(self, fileobj):
  144. mimetype = magic.from_buffer(fileobj.read(1024), mime=True)
  145. fileobj.seek(0)
  146. # Check if the binary file is a blender file
  147. if ( mimetype == "application/octet-stream" or mimetype == "application/x-gzip" ) and self.check_blend_file(fileobj):
  148. return "application/x-blender", True
  149. else:
  150. return mimetype, False
  151. def get_mimetype(self, name):
  152. """ This function return the mime-type based on .type file """
  153. try:
  154. with open(self.mediapath(name) + '.type', 'r', 16) as f:
  155. mime = f.read()
  156. return mime
  157. except IOError:
  158. return None
  159. @view_defaults(route_name='media_upload')
  160. class MediaUpload(MediaPath):
  161. def __init__(self, request):
  162. self.request = request
  163. self.media_table = self.request.matchdict.get('media_table')
  164. self.linked_id = self.request.matchdict.get('uid')
  165. if not self.linked_id.isdigit():
  166. raise HTTPBadRequest('Wrong Parameter')
  167. request.response.headers['Access-Control-Allow-Origin'] = '*'
  168. request.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS, HEAD, GET, POST, PUT, DELETE'
  169. def mediapath(self, name):
  170. return self.get_mediapath(self.media_table, self.linked_id, name)
  171. def validate(self, result, filecontent):
  172. """ Do some basic check to the uploaded file before to accept it """
  173. # Try to determine mime type from content uploaded
  174. found_mime = magic.from_buffer(filecontent.read(1024), mime=True)
  175. filecontent.seek(0)
  176. # Do a special statement for specific detected mime type
  177. if found_mime in ["application/octet-stream", "application/x-gzip"]:
  178. # Lets see if it's a bender file
  179. if self.check_blend_file(filecontent):
  180. found_mime = "application/x-blender"
  181. # MonKey Patch of content type
  182. result['type'] = found_mime
  183. # Reject mime type that don't match
  184. if found_mime!=result['type']:
  185. result['error'] = 'L\'extension du fichier ne correspond pas à son contenu - '
  186. result['error'] += "( %s vs %s )" % (found_mime, result['type'])
  187. return False
  188. # Accept images and mime types listed
  189. if not found_mime in ACCEPTED_MIMES:
  190. if not (IMAGE_TYPES.match(found_mime)):
  191. result['error'] = 'Ce type fichier n\'est malheureusement pas supporté. '
  192. return False
  193. if result['size'] < MIN_FILE_SIZE:
  194. result['error'] = 'le fichier est trop petit'
  195. elif result['size'] > MAX_FILE_SIZE:
  196. result['error'] = 'le fichier est trop voluminueux'
  197. #elif not ACCEPT_FILE_TYPES.match(file['type']):
  198. # file['error'] = u'les type de fichiers acceptés sont png, jpg et gif'
  199. else:
  200. return True
  201. return False
  202. def get_file_size(self, fileobj):
  203. fileobj.seek(0, 2) # Seek to the end of the file
  204. size = fileobj.tell() # Get the position of EOF
  205. fileobj.seek(0) # Reset the file position to the beginning
  206. return size
  207. def thumbnailurl(self,name):
  208. return self.request.route_url('media_view',name='thumbnails',
  209. media_table=self.media_table,
  210. uid=self.linked_id) + '/' + name
  211. def thumbnailpath(self,name):
  212. origin = self.mediapath(name)
  213. TargetPath = os.path.join( os.path.dirname(origin), 'thumbnails', name)
  214. if not os.path.isdir(os.path.dirname(TargetPath)):
  215. os.makedirs(os.path.dirname(TargetPath))
  216. return TargetPath
  217. def createthumbnail(self, filename):
  218. image = Image.open( self.mediapath(filename) )
  219. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  220. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  221. timage.paste(
  222. image,
  223. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  224. TargetFileName = self.thumbnailpath(filename)
  225. timage.save( TargetFileName )
  226. return self.thumbnailurl( os.path.basename(TargetFileName) )
  227. def pdfthumbnail(self, filename):
  228. TargetFileName = self.thumbnailpath(filename)
  229. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  230. Result = subprocess.call(Command)
  231. if Result==0:
  232. image = Image.open( TargetFileName+"_.jpg" )
  233. pdf_indicator = Image.open( "jm2l/static/img/PDF_Thumb_Stamp.png" )
  234. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  235. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  236. # Add thumbnail
  237. timage.paste(
  238. image,
  239. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  240. # Stamp with PDF file type
  241. timage.paste(
  242. pdf_indicator,
  243. (timage.size[0]-30, timage.size[1]-30),
  244. pdf_indicator,
  245. )
  246. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  247. os.unlink(TargetFileName+"_.jpg")
  248. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  249. return self.ExtMimeIcon('application/pdf')
  250. def svgthumbnail(self, filename):
  251. TargetFileName = self.thumbnailpath(filename)
  252. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  253. Result = subprocess.call(Command)
  254. if Result==0:
  255. image = Image.open( TargetFileName+"_.jpg" )
  256. pdf_indicator = Image.open( "jm2l/static/img/svg-icon.png" )
  257. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  258. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  259. # Add thumbnail
  260. timage.paste(
  261. image,
  262. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  263. # Stamp with PDF file type
  264. timage.paste(
  265. pdf_indicator,
  266. (timage.size[0]-30, timage.size[1]-30),
  267. pdf_indicator,
  268. )
  269. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  270. os.unlink(TargetFileName+"_.jpg")
  271. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  272. return self.ExtMimeIcon('image/svg+xml')
  273. def docthumbnail(self, filename):
  274. TargetFileName = self.thumbnailpath(filename)
  275. # let's take the thumbnail generated inside the document
  276. Command = ["unzip", "-p", self.mediapath(filename), "Thumbnails/thumbnail.png"]
  277. ThumbBytes = subprocess.check_output(Command)
  278. image = Image.open( StringIO.StringIO(ThumbBytes) )
  279. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  280. # Use the correct stamp
  281. f, ext = os.path.splitext( filename )
  282. istamp = [ ('Writer','odt'),
  283. ('Impress','odp'),
  284. ('Calc','ods'),
  285. ('Draw','odg')]
  286. stampfilename = filter(lambda (x,y): ext.endswith(y), istamp)
  287. stamp = Image.open( "jm2l/static/img/%s-icon.png" % stampfilename[0][0])
  288. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  289. # Add thumbnail
  290. timage.paste(
  291. image,
  292. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  293. # Stamp with PDF file type
  294. timage.paste(
  295. stamp,
  296. (timage.size[0]-30, timage.size[1]-30),
  297. stamp,
  298. )
  299. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  300. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  301. def blendthumbnail(self, filename):
  302. blendfile = self.mediapath(filename)
  303. # Extract Thumb
  304. if 0:
  305. head = fileobj.read(12)
  306. fileobj.seek(0)
  307. if head[:2] == b'\x1f\x8b': # gzip magic
  308. import zlib
  309. head = zlib.decompress(fileobj.read(), 31)[:12]
  310. fileobj.seek(0)
  311. buf, width, height = blend_extract_thumb(blendfile)
  312. if buf:
  313. png = write_png(buf, width, height)
  314. TargetFileName = self.thumbnailpath(filename)
  315. image = Image.open(StringIO.StringIO(png))
  316. blender_indicator = Image.open( "jm2l/static/img/Blender_Thumb_Stamp.png" )
  317. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  318. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  319. # Add thumbnail
  320. timage.paste(
  321. image,
  322. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  323. # Stamp with Blender file type
  324. timage.paste(
  325. blender_indicator,
  326. (timage.size[0]-30, timage.size[1]-30),
  327. blender_indicator,
  328. )
  329. timage.save( TargetFileName+".png")
  330. return self.thumbnailurl( os.path.basename(TargetFileName+".png") )
  331. return self.ExtMimeIcon('application/x-blender')
  332. def fileinfo(self,name):
  333. filename = self.mediapath(name)
  334. f, ext = os.path.splitext(name)
  335. if ext!='.type' and os.path.isfile(filename):
  336. info = {}
  337. info['name'] = name
  338. info['size'] = os.path.getsize(filename)
  339. info['url'] = self.request.route_url('media_view',
  340. name=name,
  341. media_table=self.media_table,
  342. uid=self.linked_id)
  343. mime = self.get_mimetype(name)
  344. if IMAGE_TYPES.match(mime):
  345. info['thumbnailUrl'] = self.thumbnailurl(name)
  346. elif mime in ACCEPTED_MIMES:
  347. thumb = self.thumbnailpath("%s%s" % (f, ext))
  348. thumbext = ".jpg"
  349. if mime == "application/x-blender":
  350. thumbext = ".png"
  351. if os.path.exists( thumb + thumbext ):
  352. info['thumbnailUrl'] = self.thumbnailurl(name)+thumbext
  353. else:
  354. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  355. else:
  356. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  357. info['deleteType'] = DELETEMETHOD
  358. info['deleteUrl'] = self.request.route_url('media_upload',
  359. sep='',
  360. name='',
  361. media_table=self.media_table,
  362. uid=self.linked_id) + '/' + name
  363. if DELETEMETHOD != 'DELETE':
  364. info['deleteUrl'] += '&_method=DELETE'
  365. return info
  366. else:
  367. return None
  368. @view_config(request_method='OPTIONS')
  369. def options(self):
  370. return Response(body='')
  371. @view_config(request_method='HEAD')
  372. def options(self):
  373. return Response(body='')
  374. @view_config(request_method='GET', renderer="json")
  375. def get(self):
  376. p = self.request.matchdict.get('name')
  377. if p:
  378. return self.fileinfo(p)
  379. else:
  380. filelist = []
  381. content = self.mediapath('')
  382. if content and path.exists(content):
  383. for f in os.listdir(content):
  384. n = self.fileinfo(f)
  385. if n:
  386. filelist.append(n)
  387. return { "files":filelist }
  388. @view_config(request_method='DELETE', xhr=True, accept="application/json", renderer='json')
  389. def delete(self):
  390. filename = self.request.matchdict.get('name')
  391. try:
  392. os.remove(self.mediapath(filename) + '.type')
  393. except IOError:
  394. pass
  395. except OSError:
  396. pass
  397. try:
  398. os.remove(self.thumbnailpath(filename))
  399. except IOError:
  400. pass
  401. except OSError:
  402. pass
  403. try:
  404. os.remove(self.thumbnailpath(filename+".jpg"))
  405. except IOError:
  406. pass
  407. except OSError:
  408. pass
  409. try:
  410. os.remove(self.mediapath(filename))
  411. except IOError:
  412. return False
  413. return True
  414. @view_config(request_method='POST', xhr=True, accept="application/json", renderer='json')
  415. def post(self):
  416. if self.request.matchdict.get('_method') == "DELETE":
  417. return self.delete()
  418. results = []
  419. for name, fieldStorage in self.request.POST.items():
  420. if isinstance(fieldStorage,unicode):
  421. continue
  422. result = {}
  423. result['name'] = os.path.basename(fieldStorage.filename)
  424. result['type'] = fieldStorage.type
  425. result['size'] = self.get_file_size(fieldStorage.file)
  426. if self.validate(result, fieldStorage.file):
  427. # Keep mime-type in .type file
  428. with open( self.mediapath( result['name'] ) + '.type', 'w') as f:
  429. f.write(result['type'])
  430. # Store uploaded file
  431. fieldStorage.file.seek(0)
  432. with open( self.mediapath(result['name'] ), 'wb') as f:
  433. shutil.copyfileobj( fieldStorage.file , f)
  434. if re.match(IMAGE_TYPES, result['type']):
  435. result['thumbnailUrl'] = self.createthumbnail(result['name'])
  436. elif result['type']=='application/pdf':
  437. result['thumbnailUrl'] = self.pdfthumbnail(result['name'])
  438. elif result['type']=='image/svg+xml':
  439. result['thumbnailUrl'] = self.svgthumbnail(result['name'])
  440. elif result['type'].startswith('application/vnd'):
  441. result['thumbnailUrl'] = self.docthumbnail(result['name'])
  442. elif result['type']=='application/x-blender':
  443. result['thumbnailUrl'] = self.blendthumbnail(result['name'])
  444. else:
  445. result['thumbnailUrl'] = self.ExtMimeIcon(result['type'])
  446. result['deleteType'] = DELETEMETHOD
  447. result['deleteUrl'] = self.request.route_url('media_upload',
  448. sep='',
  449. name='',
  450. media_table=self.media_table,
  451. uid=self.linked_id) + '/' + result['name']
  452. result['url'] = self.request.route_url('media_view',
  453. media_table=self.media_table,
  454. uid=self.linked_id,
  455. name=result['name'])
  456. if DELETEMETHOD != 'DELETE':
  457. result['deleteUrl'] += '&_method=DELETE'
  458. results.append(result)
  459. return {"files":results}
  460. @view_defaults(route_name='media_view')
  461. class MediaView(MediaPath):
  462. def __init__(self,request):
  463. self.request = request
  464. self.media_table = self.request.matchdict.get('media_table')
  465. self.linked_id = self.request.matchdict.get('uid')
  466. def mediapath(self,name):
  467. return self.get_mediapath(self.media_table, self.linked_id, name)
  468. @view_config(request_method='GET', http_cache = (EXPIRATION_TIME, {'public':True}))
  469. def get(self):
  470. name = self.request.matchdict.get('name')
  471. self.request.response.content_type = self.get_mimetype(name)
  472. try:
  473. self.request.response.body_file = open( self.mediapath(name), 'rb', 10000)
  474. except IOError:
  475. raise NotFound
  476. return self.request.response
  477. ##return Response(app_iter=ImgHandle, content_type = 'image/png')