Le repo des sources pour le site web des JM2L
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

403 righe
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 + [ 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. print "%s != %s" % (RealMime, result['type'])
  130. return False
  131. # Accept images and mime types listed
  132. if not RealMime in ACCEPTED_MIMES:
  133. if not (IMAGE_TYPES.match(RealMime)):
  134. result['error'] = 'Ce type fichier n\'est malheureusement pas supporté. '
  135. result['error'] += 'Les fichiers acceptées sont les images et pdf.'
  136. return False
  137. if result['size'] < MIN_FILE_SIZE:
  138. result['error'] = 'le fichier est trop petit'
  139. elif result['size'] > MAX_FILE_SIZE:
  140. result['error'] = 'le fichier est trop voluminueux'
  141. #elif not ACCEPT_FILE_TYPES.match(file['type']):
  142. # file['error'] = u'les type de fichiers acceptés sont png, jpg et gif'
  143. else:
  144. return True
  145. return False
  146. def get_file_size(self, file):
  147. file.seek(0, 2) # Seek to the end of the file
  148. size = file.tell() # Get the position of EOF
  149. file.seek(0) # Reset the file position to the beginning
  150. return size
  151. def thumbnailurl(self,name):
  152. return self.request.route_url('media_view',name='thumbnails',
  153. media_table=self.media_table,
  154. uid=self.linked_id) + '/' + name
  155. def thumbnailpath(self,name):
  156. origin = self.mediapath(name)
  157. TargetPath = os.path.join( os.path.dirname(origin), 'thumbnails', name)
  158. if not os.path.isdir(os.path.dirname(TargetPath)):
  159. os.makedirs(os.path.dirname(TargetPath))
  160. return TargetPath
  161. def createthumbnail(self, filename):
  162. image = Image.open( self.mediapath(filename) )
  163. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  164. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  165. timage.paste(
  166. image,
  167. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  168. TargetFileName = self.thumbnailpath(filename)
  169. timage.save( TargetFileName )
  170. return self.thumbnailurl( os.path.basename(TargetFileName) )
  171. def pdfthumbnail(self, filename):
  172. TargetFileName = self.thumbnailpath(filename)
  173. Command = ["convert","./%s[0]" % self.mediapath(filename),"./%s_.jpg" % TargetFileName]
  174. Result = subprocess.call(Command)
  175. if Result==0:
  176. image = Image.open( TargetFileName+"_.jpg" )
  177. pdf_indicator = Image.open( "jm2l/static/img/PDF_Thumb_Stamp.png" )
  178. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  179. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  180. # Add thumbnail
  181. timage.paste(
  182. image,
  183. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  184. # Stamp with PDF file type
  185. timage.paste(
  186. pdf_indicator,
  187. (timage.size[0]-30, timage.size[1]-30),
  188. pdf_indicator,
  189. )
  190. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  191. os.unlink(TargetFileName+"_.jpg")
  192. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  193. return self.ExtMimeIcon('application/pdf')
  194. def docthumbnail(self, filename):
  195. TargetFileName = self.thumbnailpath(filename)
  196. # unoconv need a libre office server to be up
  197. Command = ["unoconv", "-f", "pdf", "-e", "PageRange=1", "--output=%s" % TargetFileName, \
  198. "%s[0]" % self.mediapath(filename) ]
  199. # let's take the thumbnail generated inside the document
  200. Command = ["unzip", "-p", self.mediapath(filename), "Thumbnails/thumbnail.png"]
  201. ThumbBytes = subprocess.check_output(Command)
  202. image = Image.open( StringIO.StringIO(ThumbBytes) )
  203. image.thumbnail((THUMBNAIL_SIZE, THUMBNAIL_SIZE), Image.ANTIALIAS)
  204. # Use the correct stamp
  205. f, ext = os.path.splitext( filename )
  206. istamp = [ ('Writer','odt'),
  207. ('Impress','odp'),
  208. ('Calc','ods'),
  209. ('Draw','odg')]
  210. stampfilename = filter(lambda (x,y): ext.endswith(y), istamp)
  211. stamp = Image.open( "jm2l/static/img/%s-icon.png" % stampfilename[0][0])
  212. timage = Image.new('RGBA', (THUMBNAIL_SIZE, THUMBNAIL_SIZE), (255, 255, 255, 0))
  213. # Add thumbnail
  214. timage.paste(
  215. image,
  216. ((THUMBNAIL_SIZE - image.size[0]) / 2, (THUMBNAIL_SIZE - image.size[1]) / 2))
  217. # Stamp with PDF file type
  218. timage.paste(
  219. stamp,
  220. (timage.size[0]-30, timage.size[1]-30),
  221. stamp,
  222. )
  223. timage.convert('RGB').save( TargetFileName+".jpg", 'JPEG')
  224. return self.thumbnailurl( os.path.basename(TargetFileName+".jpg") )
  225. def fileinfo(self,name):
  226. filename = self.mediapath(name)
  227. f, ext = os.path.splitext(name)
  228. if ext!='.type' and os.path.isfile(filename):
  229. info = {}
  230. info['name'] = name
  231. info['size'] = os.path.getsize(filename)
  232. info['url'] = self.request.route_url('media_view',
  233. name=name,
  234. media_table=self.media_table,
  235. uid=self.linked_id)
  236. mime = mimetypes.types_map.get(ext)
  237. if (IMAGE_TYPES.match(mime)):
  238. info['thumbnailUrl'] = self.thumbnailurl(name)
  239. elif mime in ACCEPTED_MIMES:
  240. thumb = self.thumbnailpath("%s%s" % (f, ext))
  241. if os.path.exists( thumb +'.jpg' ):
  242. info['thumbnailUrl'] = self.thumbnailurl(name)+'.jpg'
  243. else:
  244. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  245. else:
  246. info['thumbnailUrl'] = self.ExtMimeIcon(mime)
  247. info['deleteType'] = DELETEMETHOD
  248. info['deleteUrl'] = self.request.route_url('media_upload',
  249. sep='',
  250. name='',
  251. media_table=self.media_table,
  252. uid=self.linked_id) + '/' + name
  253. if DELETEMETHOD != 'DELETE':
  254. info['deleteUrl'] += '&_method=DELETE'
  255. return info
  256. else:
  257. return None
  258. @view_config(request_method='OPTIONS')
  259. def options(self):
  260. return Response(body='')
  261. @view_config(request_method='HEAD')
  262. def options(self):
  263. return Response(body='')
  264. @view_config(request_method='GET', renderer="json")
  265. def get(self):
  266. p = self.request.matchdict.get('name')
  267. if p:
  268. return self.fileinfo(p)
  269. else:
  270. filelist = []
  271. content = self.mediapath('')
  272. if content and path.exists(content):
  273. for f in os.listdir(content):
  274. n = self.fileinfo(f)
  275. if n:
  276. filelist.append(n)
  277. return { "files":filelist }
  278. @view_config(request_method='DELETE', xhr=True, accept="application/json", renderer='json')
  279. def delete(self):
  280. import json
  281. filename = self.request.matchdict.get('name')
  282. try:
  283. os.remove(self.mediapath(filename) + '.type')
  284. except IOError:
  285. pass
  286. except OSError:
  287. pass
  288. try:
  289. os.remove(self.thumbnailpath(filename))
  290. except IOError:
  291. pass
  292. except OSError:
  293. pass
  294. try:
  295. os.remove(self.thumbnailpath(filename+".jpg"))
  296. except IOError:
  297. pass
  298. except OSError:
  299. pass
  300. try:
  301. os.remove(self.mediapath(filename))
  302. except IOError:
  303. return False
  304. return True
  305. @view_config(request_method='POST', xhr=True, accept="application/json", renderer='json')
  306. def post(self):
  307. if self.request.matchdict.get('_method') == "DELETE":
  308. return self.delete()
  309. results = []
  310. for name, fieldStorage in self.request.POST.items():
  311. if isinstance(fieldStorage,unicode):
  312. continue
  313. result = {}
  314. result['name'] = os.path.basename(fieldStorage.filename)
  315. result['type'] = fieldStorage.type
  316. result['size'] = self.get_file_size(fieldStorage.file)
  317. print result
  318. if self.validate(result, fieldStorage.file):
  319. with open( self.mediapath(result['name'] + '.type'), 'w') as f:
  320. f.write(result['type'])
  321. with open( self.mediapath(result['name']), 'w') as f:
  322. shutil.copyfileobj( fieldStorage.file , f)
  323. if re.match(IMAGE_TYPES, result['type']):
  324. result['thumbnailUrl'] = self.createthumbnail(result['name'])
  325. elif result['type']=='application/pdf':
  326. result['thumbnailUrl'] = self.pdfthumbnail(result['name'])
  327. elif result['type'].startswith('application/vnd'):
  328. result['thumbnailUrl'] = self.docthumbnail(result['name'])
  329. else:
  330. result['thumbnailUrl'] = self.ExtMimeIcon(result['type'])
  331. result['deleteType'] = DELETEMETHOD
  332. result['deleteUrl'] = self.request.route_url('media_upload',
  333. sep='',
  334. name='',
  335. media_table=self.media_table,
  336. uid=self.linked_id) + '/' + result['name']
  337. result['url'] = self.request.route_url('media_view',
  338. media_table=self.media_table,
  339. uid=self.linked_id,
  340. name=result['name'])
  341. if DELETEMETHOD != 'DELETE':
  342. result['deleteUrl'] += '&_method=DELETE'
  343. results.append(result)
  344. return {"files":results}
  345. @view_defaults(route_name='media_view')
  346. class MediaView(MediaPath):
  347. def __init__(self,request):
  348. self.request = request
  349. self.media_table = self.request.matchdict.get('media_table')
  350. self.linked_id = self.request.matchdict.get('uid')
  351. def mediapath(self,name):
  352. return self.get_mediapath(self.media_table, self.linked_id, name)
  353. @view_config(request_method='GET') #, http_cache = (EXPIRATION_TIME, {'public':True}))
  354. def get(self):
  355. name = self.request.matchdict.get('name')
  356. try:
  357. with open( self.mediapath( os.path.basename(name) ) + '.type', 'r', 16) as f:
  358. test = f.read()
  359. self.request.response.content_type = test
  360. except IOError:
  361. print os.path.basename(name)+".type file not found"
  362. pass
  363. #try:
  364. if 1:
  365. self.request.response.body_file = open( self.mediapath(name), 'r', 10000)
  366. #except IOError:
  367. # raise NotFound
  368. return self.request.response
  369. ##return Response(app_iter=ImgHandle, content_type = 'image/png')