Mercurial > libervia-backend
diff src/plugins/plugin_comp_file_sharing.py @ 2514:4440ea7047bd
file sharing component: thumbnails integration first draft:
if an uploaded file is detected as an image, 2 thumbnails (small and medium size) are generated automatically.
author | Goffi <goffi@goffi.org> |
---|---|
date | Fri, 02 Mar 2018 17:53:31 +0100 |
parents | 67cc54b01a12 |
children | 353880a5c363 |
line wrap: on
line diff
--- a/src/plugins/plugin_comp_file_sharing.py Fri Mar 02 17:53:31 2018 +0100 +++ b/src/plugins/plugin_comp_file_sharing.py Fri Mar 02 17:53:31 2018 +0100 @@ -27,6 +27,7 @@ from twisted.internet import defer import os import os.path +import mimetypes PLUGIN_INFO = { @@ -35,14 +36,13 @@ C.PI_MODES: [C.PLUG_MODE_COMPONENT], C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT, C.PI_PROTOCOLS: [], - C.PI_DEPENDENCIES: ["FILE", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0329"], + C.PI_DEPENDENCIES: ["FILE", "XEP-0234", "XEP-0260", "XEP-0261", "XEP-0264", "XEP-0329"], C.PI_RECOMMENDATIONS: [], C.PI_MAIN: "FileSharing", C.PI_HANDLER: "no", C.PI_DESCRIPTION: _(u"""Component hosting and sharing files""") } -PROGRESS_ID_KEY = 'progress_id' HASH_ALGO = u'sha-256' @@ -54,6 +54,7 @@ self._f = host.plugins['FILE'] self._jf = host.plugins['XEP-0234'] self._h = host.plugins['XEP-0300'] + self._t = host.plugins['XEP-0264'] host.trigger.add("FILE_getDestDir", self._getDestDirTrigger) host.trigger.add("XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000) self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False) @@ -68,6 +69,9 @@ @defer.inlineCallbacks def _fileTransferedCb(self, dummy, client, peer_jid, file_data, file_path): + name = file_data[u'name'] + extra = {} + if file_data[u'hash_algo'] == HASH_ALGO: log.debug(_(u"Reusing already generated hash")) file_hash = file_data[u'hash_hasher'].hexdigest() @@ -76,28 +80,49 @@ with open('file_path') as f: file_hash = yield self._h.calculateHash(f, hasher) final_path = os.path.join(self.files_path, file_hash) + if os.path.isfile(final_path): log.debug(u"file [{file_hash}] already exists, we can remove temporary one".format(file_hash = file_hash)) os.unlink(file_path) else: os.rename(file_path, final_path) log.debug(u"file [{file_hash}] moved to {files_path}".format(file_hash=file_hash, files_path=self.files_path)) + + mime_type = file_data.get(u'mime_type') + if not mime_type or mime_type == u'application/octet-stream': + mime_type = mimetypes.guess_type(name)[0] + + if mime_type is not None and mime_type.startswith(u'image'): + thumbnails = extra.setdefault(C.KEY_THUMBNAILS, []) + for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM): + try: + thumb_size, thumb_id = yield self._t.generateThumbnail(final_path, + max_thumb_size, + # we keep thumbnails for 6 months + 60*60*24*31*6) + except Exception as e: + log.warning(_(u"Can't create thumbnail: {reason}").format(reason=e)) + break + thumbnails.append({u'id': thumb_id, u'size': thumb_size}) + self.host.memory.setFile(client, - name=file_data[u'name'], + name=name, version=u'', file_hash=file_hash, hash_algo=HASH_ALGO, size=file_data[u'size'], path=file_data.get(u'path'), namespace=file_data.get(u'namespace'), - owner=peer_jid) + mime_type=mime_type, + owner=peer_jid, + extra=extra) def _getDestDirTrigger(self, client, peer_jid, transfer_data, file_data, stream_object): if not client.is_component: return True, None assert stream_object assert 'stream_object' not in transfer_data - assert PROGRESS_ID_KEY in file_data + assert C.KEY_PROGRESS_ID in file_data filename = file_data['name'] assert filename and not '/' in filename file_tmp_dir = self.host.getLocalPath(client, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False)