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.
 
 
 
 
 

427 lines
17 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.request import Request
  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
  15. MIN_FILE_SIZE = 1 # bytes
  16. MAX_FILE_SIZE = 500000000 # bytes
  17. IMAGE_TYPES = re.compile('image/(gif|p?jpeg|(x-)?png)')
  18. ACCEPTED_MIMES = ['application/pdf',
  19. 'application/vnd.oasis.opendocument.text',
  20. 'application/vnd.oasis.opendocument.text-template',
  21. 'application/vnd.oasis.opendocument.graphics',
  22. 'application/vnd.oasis.opendocument.graphics-template',
  23. 'application/vnd.oasis.opendocument.presentation',
  24. 'application/vnd.oasis.opendocument.presentation-template',
  25. 'application/vnd.oasis.opendocument.spreadsheet',
  26. 'application/vnd.oasis.opendocument.spreadsheet-template',
  27. 'image/svg+xml'
  28. ]
  29. ACCEPT_FILE_TYPES = IMAGE_TYPES
  30. THUMBNAIL_SIZE = 80
  31. EXPIRATION_TIME = 300 # seconds
  32. IMAGEPATH = [ 'images' ]
  33. DOCPATH = [ 'document' ]
  34. THUMBNAILPATH = [ 'images', 'thumbnails' ]
  35. # change the following to POST if DELETE isn't supported by the webserver
  36. DELETEMETHOD="DELETE"
  37. mimetypes.init()
  38. class MediaPath():
  39. def get_list(self, media_table, linked_id):
  40. filelist = list()
  41. curpath = self.get_mediapath(media_table, linked_id, None)
  42. if not os.path.isdir(curpath):
  43. return list()
  44. for f in os.listdir(curpath):
  45. if os.path.isdir(os.path.join(curpath,f)):
  46. continue
  47. if f.endswith('.type'):
  48. continue
  49. if f:
  50. tmpurl = '/image/%s/%d/%s' % (media_table, linked_id, f)
  51. filelist.append(tmpurl)
  52. return filelist
  53. def get_thumb(self, media_table, linked_id):
  54. filelist = list()
  55. curpath = self.get_mediapath(media_table, linked_id, None)
  56. curpath = os.path.join( curpath, 'thumbnails')
  57. if not os.path.isdir(curpath):
  58. return list()
  59. for f in os.listdir(curpath):
  60. if os.path.isdir(os.path.join(curpath,f)):
  61. continue
  62. if f.endswith('.type'):
  63. continue
  64. if f:
  65. tmpurl = '/image/%s/%d/thumbnails/%s' % (media_table, linked_id, f)
  66. filelist.append(tmpurl)
  67. return filelist
  68. def get_mediapath(self, media_table, linked_id, name):
  69. linked_id = str(linked_id)
  70. if media_table in ['tiers', 'place', 'salle']:
  71. # Retrieve Slug
  72. if media_table=='tiers':
  73. slug = Tiers.by_id(linked_id).slug
  74. if media_table=='place':
  75. slug = Place.by_id(linked_id).slug
  76. if media_table=='salle':
  77. phyid = Salles.by_id(linked_id).phy_salle_id
  78. if phyid:
  79. slug = SallePhy.by_id(phyid)
  80. else:
  81. slug = linked_id
  82. p = IMAGEPATH + [ media_table ] + [ slug ]
  83. elif media_table=='presse':
  84. # Use Year in linked_id
  85. p = IMAGEPATH + [ media_table ] + [ linked_id ]
  86. elif media_table=='tasks':
  87. # Use Current Year
  88. p = IMAGEPATH + [ str(2015), media_table ] + [ linked_id ]
  89. elif media_table=='poles':
  90. # Use Current Year
  91. p = IMAGEPATH + [ str(2015), media_table ] + [ linked_id ]
  92. elif media_table in ['RIB', 'Justif']:
  93. slug = User.by_id(linked_id).slug
  94. p = IMAGEPATH + ['users'] + [ slug ] + [ self.media_table ]
  95. elif media_table=='users':
  96. slug = User.by_id(linked_id).slug
  97. p = IMAGEPATH + ['users'] + [ slug ]
  98. elif media_table=='event':
  99. ev = Event.by_id(linked_id)
  100. slug = ev.slug
  101. year = ev.for_year
  102. p = IMAGEPATH + ['event'] + [ str(year) ] + [ slug ]
  103. if name:
  104. p += [ name ]
  105. TargetPath = os.path.join('jm2l/upload', *p)
  106. if not os.path.isdir(os.path.dirname(TargetPath)):
  107. os.makedirs(os.path.dirname(TargetPath))
  108. return os.path.join('jm2l/upload', *p)
  109. def ExtMimeIcon(self, mime):
  110. if mime=='application/pdf':
  111. return "/img/PDF.png"
  112. @view_defaults(route_name='media_upload')
  113. class MediaUpload(MediaPath):
  114. def __init__(self, request):
  115. self.request = request
  116. self.media_table = self.request.matchdict.get('media_table')
  117. self.linked_id = self.request.matchdict.get('uid')
  118. if not self.linked_id.isdigit():
  119. raise HTTPBadRequest('Wrong Parameter')
  120. request.response.headers['Access-Control-Allow-Origin'] = '*'
  121. request.response.headers['Access-Control-Allow-Methods'] = 'OPTIONS, HEAD, GET, POST, PUT, DELETE'
  122. def mediapath(self, name):
  123. return self.get_mediapath(self.media_table, self.linked_id, name)
  124. def validate(self, result, filecontent):
  125. # let's say we don't trust the uploader
  126. RealMime = magic.from_buffer( filecontent.read(1024), mime=True)
  127. filecontent.seek(0)
  128. if RealMime!=result['type']:
  129. result['error'] = 'l\'extension du fichier ne correspond pas à son contenu - '
  130. result['error'] += "( %s vs %s )" % (RealMime, result['type'])
  131. return False
  132. # Accept images and mime types listed
  133. if not RealMime in ACCEPTED_MIMES:
  134. if not (IMAGE_TYPES.match(RealMime)):
  135. result['error'] = 'Ce type fichier n\'est malheureusement pas supporté. '
  136. result['error'] += 'Les fichiers acceptées sont les images et pdf.'
  137. return False
  138. if result['size'] < MIN_FILE_SIZE:
  139. result['error'] = 'le fichier est trop petit'
  140. elif result['size'] > MAX_FILE_SIZE:
  141. result['error'] = 'le fichier est trop voluminueux'
  142. #elif not ACCEPT_FILE_TYPES.match(file['type']):
  143. # file['error'] = u'les type de fichiers acceptés sont png, jpg et gif'
  144. else:
  145. return True
  146. return False
  147. def get_file_size(self, file):
  148. file.seek(0, 2) # Seek to the end of the file
  149. size = file.tell() # Get the position of EOF
  150. file.seek(0) # Reset the file position to the beginning
  151. return size
  152. def thumbnailurl(self,name):
  153. return self.request.route_url('media_view',name='thumbnails',
  154. media_table=self.media_table,
  155. uid=self.linked_id) + '/' + name
  156. def thumbnailpath(self,name):
  157. origin = self.mediapath(name)
  158. TargetPath = os.path.join( os.path.dirname(origin), 'thumbnails', name)
  159. if not os.path.isdir(os.path.dirname(TargetPath)):
  160. os.makedirs(os.path.dirname(TargetPath))
  161. return TargetPath
  162. def createthumbnail(self, filename):
  163. image = Image.open( self.mediapath(filename) )
  164. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  165. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  166. timage.paste(
  167. image,
  168. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  169. TargetFileName = self.thumbnailpath(filename)
  170. timage.save( TargetFileName )
  171. return self.thumbnailurl( os.path.basename(TargetFileName) )
  172. def pdfthumbnail(self, filename):
  173. TargetFileName = self.thumbnailpath(filename)
  174. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  175. Result = subprocess.call(Command)
  176. if Result==0:
  177. image = Image.open( TargetFileName+"_.jpg" )
  178. pdf_indicator = Image.open( "jm2l/static/img/PDF_Thumb_Stamp.png" )
  179. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  180. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  181. # Add thumbnail
  182. timage.paste(
  183. image,
  184. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  185. # Stamp with PDF file type
  186. timage.paste(
  187. pdf_indicator,
  188. (timage.size[0]-30, timage.size[1]-30),
  189. pdf_indicator,
  190. )
  191. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  192. os.unlink(TargetFileName+"_.jpg")
  193. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  194. return self.ExtMimeIcon('application/pdf')
  195. def svgthumbnail(self, filename):
  196. TargetFileName = self.thumbnailpath(filename)
  197. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  198. Result = subprocess.call(Command)
  199. if Result==0:
  200. image = Image.open( TargetFileName+"_.jpg" )
  201. pdf_indicator = Image.open( "jm2l/static/img/svg-icon.png" )
  202. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  203. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  204. # Add thumbnail
  205. timage.paste(
  206. image,
  207. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  208. # Stamp with PDF file type
  209. timage.paste(
  210. pdf_indicator,
  211. (timage.size[0]-30, timage.size[1]-30),
  212. pdf_indicator,
  213. )
  214. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  215. os.unlink(TargetFileName+"_.jpg")
  216. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  217. return self.ExtMimeIcon('image/svg+xml')
  218. def docthumbnail(self, filename):
  219. TargetFileName = self.thumbnailpath(filename)
  220. # unoconv need a libre office server to be up
  221. Command = ["unoconv", "-f", "pdf", "-e", "PageRange=1", "--output=%s" % TargetFileName, \
  222. "%s[0]" % self.mediapath(filename) ]
  223. # let's take the thumbnail generated inside the document
  224. Command = ["unzip", "-p", self.mediapath(filename), "Thumbnails/thumbnail.png"]
  225. ThumbBytes = subprocess.check_output(Command)
  226. image = Image.open( StringIO.StringIO(ThumbBytes) )
  227. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  228. # Use the correct stamp
  229. f, ext = os.path.splitext( filename )
  230. istamp = [ ('Writer','odt'),
  231. ('Impress','odp'),
  232. ('Calc','ods'),
  233. ('Draw','odg')]
  234. stampfilename = filter(lambda (x,y): ext.endswith(y), istamp)
  235. stamp = Image.open( "jm2l/static/img/%s-icon.png" % stampfilename[0][0])
  236. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  237. # Add thumbnail
  238. timage.paste(
  239. image,
  240. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  241. # Stamp with PDF file type
  242. timage.paste(
  243. stamp,
  244. (timage.size[0]-30, timage.size[1]-30),
  245. stamp,
  246. )
  247. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  248. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  249. def fileinfo(self,name):
  250. filename = self.mediapath(name)
  251. f, ext = os.path.splitext(name)
  252. if ext!='.type' and os.path.isfile(filename):
  253. info = {}
  254. info['name'] = name
  255. info['size'] = os.path.getsize(filename)
  256. info['url'] = self.request.route_url('media_view',
  257. name=name,
  258. media_table=self.media_table,
  259. uid=self.linked_id)
  260. mime = mimetypes.types_map.get(ext)
  261. if (IMAGE_TYPES.match(mime)):
  262. info['thumbnailUrl'] = self.thumbnailurl(name)
  263. elif mime in ACCEPTED_MIMES:
  264. thumb = self.thumbnailpath("%s%s" % (f, ext))
  265. if os.path.exists( thumb +'.jpg' ):
  266. info['thumbnailUrl'] = self.thumbnailurl(name)+'.jpg'
  267. else:
  268. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  269. else:
  270. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  271. info['deleteType'] = DELETEMETHOD
  272. info['deleteUrl'] = self.request.route_url('media_upload',
  273. sep='',
  274. name='',
  275. media_table=self.media_table,
  276. uid=self.linked_id) + '/' + name
  277. if DELETEMETHOD != 'DELETE':
  278. info['deleteUrl'] += '&_method=DELETE'
  279. return info
  280. else:
  281. return None
  282. @view_config(request_method='OPTIONS')
  283. def options(self):
  284. return Response(body='')
  285. @view_config(request_method='HEAD')
  286. def options(self):
  287. return Response(body='')
  288. @view_config(request_method='GET', renderer="json")
  289. def get(self):
  290. p = self.request.matchdict.get('name')
  291. if p:
  292. return self.fileinfo(p)
  293. else:
  294. filelist = []
  295. content = self.mediapath('')
  296. if content and path.exists(content):
  297. for f in os.listdir(content):
  298. n = self.fileinfo(f)
  299. if n:
  300. filelist.append(n)
  301. return { "files":filelist }
  302. @view_config(request_method='DELETE', xhr=True, accept="application/json", renderer='json')
  303. def delete(self):
  304. import json
  305. filename = self.request.matchdict.get('name')
  306. try:
  307. os.remove(self.mediapath(filename) + '.type')
  308. except IOError:
  309. pass
  310. except OSError:
  311. pass
  312. try:
  313. os.remove(self.thumbnailpath(filename))
  314. except IOError:
  315. pass
  316. except OSError:
  317. pass
  318. try:
  319. os.remove(self.thumbnailpath(filename+".jpg"))
  320. except IOError:
  321. pass
  322. except OSError:
  323. pass
  324. try:
  325. os.remove(self.mediapath(filename))
  326. except IOError:
  327. return False
  328. return True
  329. @view_config(request_method='POST', xhr=True, accept="application/json", renderer='json')
  330. def post(self):
  331. if self.request.matchdict.get('_method') == "DELETE":
  332. return self.delete()
  333. results = []
  334. for name, fieldStorage in self.request.POST.items():
  335. if isinstance(fieldStorage,unicode):
  336. continue
  337. result = {}
  338. result['name'] = os.path.basename(fieldStorage.filename)
  339. result['type'] = fieldStorage.type
  340. result['size'] = self.get_file_size(fieldStorage.file)
  341. if self.validate(result, fieldStorage.file):
  342. with open( self.mediapath(result['name'] + '.type'), 'w') as f:
  343. f.write(result['type'])
  344. with open( self.mediapath(result['name']), 'wb') as f:
  345. shutil.copyfileobj( fieldStorage.file , f)
  346. if re.match(IMAGE_TYPES, result['type']):
  347. result['thumbnailUrl'] = self.createthumbnail(result['name'])
  348. elif result['type']=='application/pdf':
  349. result['thumbnailUrl'] = self.pdfthumbnail(result['name'])
  350. elif result['type']=='image/svg+xml':
  351. result['thumbnailUrl'] = self.svgthumbnail(result['name'])
  352. elif result['type'].startswith('application/vnd'):
  353. result['thumbnailUrl'] = self.docthumbnail(result['name'])
  354. else:
  355. result['thumbnailUrl'] = self.ExtMimeIcon(result['type'])
  356. result['deleteType'] = DELETEMETHOD
  357. result['deleteUrl'] = self.request.route_url('media_upload',
  358. sep='',
  359. name='',
  360. media_table=self.media_table,
  361. uid=self.linked_id) + '/' + result['name']
  362. result['url'] = self.request.route_url('media_view',
  363. media_table=self.media_table,
  364. uid=self.linked_id,
  365. name=result['name'])
  366. if DELETEMETHOD != 'DELETE':
  367. result['deleteUrl'] += '&_method=DELETE'
  368. results.append(result)
  369. return {"files":results}
  370. @view_defaults(route_name='media_view')
  371. class MediaView(MediaPath):
  372. def __init__(self,request):
  373. self.request = request
  374. self.media_table = self.request.matchdict.get('media_table')
  375. self.linked_id = self.request.matchdict.get('uid')
  376. def mediapath(self,name):
  377. return self.get_mediapath(self.media_table, self.linked_id, name)
  378. @view_config(request_method='GET') #, http_cache = (EXPIRATION_TIME, {'public':True}))
  379. def get(self):
  380. name = self.request.matchdict.get('name')
  381. try:
  382. with open( self.mediapath( os.path.basename(name) ) + '.type', 'r', 16) as f:
  383. test = f.read()
  384. self.request.response.content_type = test
  385. except IOError:
  386. pass
  387. #try:
  388. if 1:
  389. self.request.response.body_file = open( self.mediapath(name), 'rb', 10000)
  390. #except IOError:
  391. # raise NotFound
  392. return self.request.response
  393. ##return Response(app_iter=ImgHandle, content_type = 'image/png')