Le repo des sources pour le site web des JM2L
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 

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