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.
 
 
 
 
 

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