Le repo des sources pour le site web des JM2L
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 
 
 

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