comparison sat/plugins/plugin_comp_file_sharing.py @ 3331:b1e9f17fbb5a

comp file sharing: minor core re-organisation
author Goffi <goffi@goffi.org>
date Thu, 13 Aug 2020 23:46:18 +0200
parents 15612c0fb421
children 2ad14b834730
comparison
equal deleted inserted replaced
3330:7b47f48d31f3 3331:b1e9f17fbb5a
26 from pathlib import Path 26 from pathlib import Path
27 from sat.core.i18n import _ 27 from sat.core.i18n import _
28 from sat.core.constants import Const as C 28 from sat.core.constants import Const as C
29 from sat.core import exceptions 29 from sat.core import exceptions
30 from sat.core.log import getLogger 30 from sat.core.log import getLogger
31 from sat.tools import stream
31 from sat.tools.common import regex 32 from sat.tools.common import regex
32 from sat.tools.common import uri 33 from sat.tools.common import uri
33 from sat.tools.common import files_utils 34 from sat.tools.common import files_utils
34 from sat.tools.common import tls 35 from sat.tools.common import tls
35 from sat.tools import stream
36 from twisted.internet import defer, reactor 36 from twisted.internet import defer, reactor
37 from twisted.words.protocols.jabber import error 37 from twisted.words.protocols.jabber import error
38 from twisted.web import server, resource, static, http 38 from twisted.web import server, resource, static, http
39 from wokkel import pubsub 39 from wokkel import pubsub
40 from wokkel import generic 40 from wokkel import generic
50 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT, 50 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
51 C.PI_PROTOCOLS: [], 51 C.PI_PROTOCOLS: [],
52 C.PI_DEPENDENCIES: [ 52 C.PI_DEPENDENCIES: [
53 "FILE", 53 "FILE",
54 "FILE_SHARING_MANAGEMENT", 54 "FILE_SHARING_MANAGEMENT",
55 "XEP-0231",
56 "XEP-0234", 55 "XEP-0234",
57 "XEP-0260", 56 "XEP-0260",
58 "XEP-0261", 57 "XEP-0261",
59 "XEP-0264", 58 "XEP-0264",
60 "XEP-0329", 59 "XEP-0329",
402 """ 401 """
403 name = file_data["name"] 402 name = file_data["name"]
404 if extra is None: 403 if extra is None:
405 extra = {} 404 extra = {}
406 405
406 mime_type = file_data.get("mime_type")
407 if not mime_type or mime_type == "application/octet-stream":
408 mime_type = mimetypes.guess_type(name)[0]
409
410 is_image = mime_type is not None and mime_type.startswith("image")
411
407 if file_data.get("hash_algo") == HASH_ALGO: 412 if file_data.get("hash_algo") == HASH_ALGO:
408 log.debug(_("Reusing already generated hash")) 413 log.debug(_("Reusing already generated hash"))
409 file_hash = file_data["hash_hasher"].hexdigest() 414 file_hash = file_data["hash_hasher"].hexdigest()
410 else: 415 else:
411 hasher = self._h.getHasher(HASH_ALGO) 416 hasher = self._h.getHasher(HASH_ALGO)
426 "file [{file_hash}] moved to {files_path}".format( 431 "file [{file_hash}] moved to {files_path}".format(
427 file_hash=file_hash, files_path=self.files_path 432 file_hash=file_hash, files_path=self.files_path
428 ) 433 )
429 ) 434 )
430 435
431 mime_type = file_data.get("mime_type") 436
432 if not mime_type or mime_type == "application/octet-stream": 437 if is_image:
433 mime_type = mimetypes.guess_type(name)[0]
434
435 if mime_type is not None and mime_type.startswith("image"):
436 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, []) 438 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
437 for max_thumb_size in self._t.SIZES: 439 for max_thumb_size in self._t.SIZES:
438 try: 440 try:
439 thumb_size, thumb_id = await self._t.generateThumbnail( 441 thumb_size, thumb_id = await self._t.generateThumbnail(
440 final_path, 442 final_path,