annotate sat/plugins/plugin_comp_file_sharing.py @ 3289:9057713ab124

plugin comp file sharing: files can now be uploaded/downloaded via HTTP: plugin XEP-0363 can now be used by components, and file sharing uses it. The new `public_id` file metadata is used to serve files. Files uploaded are put in the `/uploads` path.
author Goffi <goffi@goffi.org>
date Fri, 29 May 2020 21:55:45 +0200
parents 9d0df638c8b4
children 449dfbfcdbcc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
1 #!/usr/bin/env python3
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
2
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
3 # SAT plugin for parrot mode (experimental)
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
4 # Copyright (C) 2009-2020 Jérôme Poisson (goffi@goffi.org)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
5
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
6 # This program is free software: you can redistribute it and/or modify
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
9 # (at your option) any later version.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
10
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
11 # This program is distributed in the hope that it will be useful,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
14 # GNU Affero General Public License for more details.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
15
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
18
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
19 import os
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
20 import os.path
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
21 import mimetypes
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
22 from functools import partial
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
23 import shortuuid
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
24 import unicodedata
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
25 from urllib.parse import urljoin, urlparse, quote, unquote
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
26 from dataclasses import dataclass
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
27 from pathlib import Path
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.core.i18n import _
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
29 from sat.core.constants import Const as C
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
30 from sat.core import exceptions
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from sat.core.log import getLogger
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
32 from sat.tools.common import regex
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
33 from sat.tools.common import uri
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
34 from sat.tools.common import files_utils
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
35 from sat.tools.common import tls
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from sat.tools import stream
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
37 from twisted.internet import defer, reactor
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
38 from twisted.words.protocols.jabber import error
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
39 from twisted.web import server, resource, static
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
40 from wokkel import pubsub
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
41 from wokkel import generic
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
42
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
43
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
44 log = getLogger(__name__)
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
45
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
46
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
47 PLUGIN_INFO = {
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
48 C.PI_NAME: "File sharing component",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
49 C.PI_IMPORT_NAME: "file_sharing",
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
52 C.PI_PROTOCOLS: [],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
53 C.PI_DEPENDENCIES: [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
54 "FILE",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
55 "FILE_SHARING_MANAGEMENT",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 "XEP-0231",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 "XEP-0234",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
58 "XEP-0260",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
59 "XEP-0261",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 "XEP-0264",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 "XEP-0329",
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
62 "XEP-0363",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 ],
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
64 C.PI_RECOMMENDATIONS: [],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
65 C.PI_MAIN: "FileSharing",
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
66 C.PI_HANDLER: C.BOOL_TRUE,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
67 C.PI_DESCRIPTION: _("""Component hosting and sharing files"""),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 }
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
69
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
70 HASH_ALGO = "sha-256"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
71 NS_COMMENTS = "org.salut-a-toi.comments"
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
72 COMMENT_NODE_PREFIX = "org.salut-a-toi.file_comments/"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
73 # Directory used to buffer request body (i.e. file in case of PUT) we use more than one @
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
74 # there, to be sure than it's not conflicting with a JID
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
75 TMP_BUFFER_DIR = "@@tmp@@"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
76
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
77 server.version = unicodedata.normalize(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
78 'NFKD',
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
79 f"{C.APP_NAME} file sharing {C.APP_VERSION}"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
80 ).encode('ascii','ignore')
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
81
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
82
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
83 class HTTPFileServer(resource.Resource):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
84 isLeaf = True
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
85
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
86 def errorPage(self, request, code):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
87 request.setResponseCode(code)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
88 if code == 400:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
89 brief = 'Bad Request'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
90 details = "Your request is invalid"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
91 elif code == 403:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
92 brief = 'Forbidden'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
93 details = "You're not allowed to use this resource"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
94 elif code == 404:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
95 brief = 'Not Found'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
96 details = "No resource found at this URL"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
97 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
98 brief = 'Error'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
99 details = "This resource can't be used"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
100 log.error(f"Unexpected return code used: {code}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
101 log.warning(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
102 f'Error returned while trying to access url {request.uri.decode()}: '
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
103 f'"{brief}" ({code}): {details}'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
104 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
105
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
106 return resource.ErrorPage(code, brief, details).render(request)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
107
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
108 def getDispositionType(self, media_type, media_subtype):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
109 if media_type in ('image', 'video'):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
110 return 'inline'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
111 elif media_type == 'application' and media_subtype == 'pdf':
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
112 return 'inline'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
113 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
114 return 'attachment'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
115
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
116 def render_GET(self, request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
117 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
118 request.upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
119 except exceptions.DataError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
120 return self.errorPage(request, 404)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
121
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
122 defer.ensureDeferred(self.renderGet(request))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
123 return server.NOT_DONE_YET
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
124
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
125 async def renderGet(self, request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
126 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
127 upload_id, filename = request.upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
128 except exceptions.DataError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
129 request.write(self.errorPage(request, 403))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
130 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
131 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
132 found_files = await request.file_sharing.host.memory.getFiles(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
133 client=None, peer_jid=None, perms_to_check=None, public_id=upload_id)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
134 if not found_files:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
135 request.write(self.errorPage(request, 404))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
136 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
137 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
138 if len(found_files) > 1:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
139 log.error(f"more that one files found for public id {upload_id!r}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
140
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
141 found_file = found_files[0]
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
142 file_path = request.file_sharing.files_path/found_file['file_hash']
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
143 file_res = static.File(file_path)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
144 file_res.type = f'{found_file["media_type"]}/{found_file["media_subtype"]}'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
145 file_res.encoding = file_res.contentEncodings.get(Path(found_file['name']).suffix)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
146 disp_type = self.getDispositionType(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
147 found_file['media_type'], found_file['media_subtype'])
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
148 # the URL is percent encoded, and not all browsers/tools unquote the file name,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
149 # thus we add a content disposition header
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
150 request.setHeader(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
151 'Content-Disposition',
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
152 f"{disp_type}; filename*=UTF-8''{quote(found_file['name'])}"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
153 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
154 ret = file_res.render(request)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
155 if ret != server.NOT_DONE_YET:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
156 # HEAD returns directly the result (while GET use a produced)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
157 request.write(ret)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
158 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
159
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
160 def render_PUT(self, request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
161 defer.ensureDeferred(self.renderPut(request))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
162 return server.NOT_DONE_YET
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
163
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
164 async def renderPut(self, request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
165 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
166 client, upload_request = request.upload_request_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
167 upload_id, filename = request.upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
168 except AttributeError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
169 request.write(self.errorPage(request, 400))
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
170 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
171 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
172
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
173 # at this point request is checked and file is buffered, we can store it
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
174 # we close the content here, before registering the file
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
175 request.content.close()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
176 tmp_file_path = Path(request.content.name)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
177 request.content = None
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
178
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
179 file_data = {
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
180 "name": unquote(upload_request.filename),
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
181 "mime_type": upload_request.content_type,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
182 "size": upload_request.size,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
183 "path": "/uploads"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
184 }
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
185
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
186 await request.file_sharing.registerReceivedFile(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
187 client, upload_request.from_, file_data, tmp_file_path,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
188 public_id=upload_id,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
189 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
190
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
191 request.setResponseCode(201)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
192 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
193
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
194
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
195 class FileSharingRequest(server.Request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
196
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
197 def __init__(self, *args, **kwargs):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
198 super().__init__(*args, **kwargs)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
199 self._upload_data = None
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
200
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
201 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
202 def upload_data(self):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
203 """A tuple with upload_id and filename retrieve from requested path"""
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
204 if self._upload_data is not None:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
205 return self._upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
206
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
207 # self.path is not available if we are easly in the request (e.g. when gotLength
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
208 # is called), in which case channel._path must be used. On the other hand, when
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
209 # render_[VERB] is called, only self.path is available
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
210 path = self.channel._path if self.path is None else self.path
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
211 # we normalise the path
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
212 path = urlparse(path.decode()).path
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
213 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
214 __, upload_id, filename = path.split('/')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
215 except ValueError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
216 raise exceptions.DataError("no enought path elements")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
217 if len(upload_id) < 10:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
218 raise exceptions.DataError(f"invalid upload ID received for a PUT: {upload_id!r}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
219
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
220 self._upload_data = (upload_id, filename)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
221 return self._upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
222
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
223 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
224 def file_sharing(self):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
225 return self.channel.site.file_sharing
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
226
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
227 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
228 def file_tmp_dir(self):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
229 return self.channel.site.file_tmp_dir
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
230
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
231 def refuseRequest(self):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
232 if self.content is not None:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
233 self.content.close()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
234 self.content = open(os.devnull, 'w+b')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
235 self.channel._respondToBadRequestAndDisconnect()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
236
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
237 def gotLength(self, length):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
238 if self.channel._command.decode().upper() == 'PUT':
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
239 # for PUT we check early if upload_id is fine, to avoid buffering a file we'll refuse
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
240 # we buffer the file in component's TMP_BUFFER_DIR, so we just have to rename it at the end
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
241 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
242 upload_id, filename = self.upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
243 except exceptions.DataError as e:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
244 log.warning("Invalid PUT request, we stop here: {e}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
245 return self.refuseRequest()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
246 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
247 client, upload_request, timer = self.file_sharing.expected_uploads.pop(upload_id)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
248 except KeyError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
249 log.warning(f"unknown (expired?) upload ID received for a PUT: {upload_id!r}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
250 return self.refuseRequest()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
251
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
252 if not timer.active:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
253 log.warning(f"upload id {upload_id!r} used for a PUT, but it is expired")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
254 return self.refuseRequest()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
255
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
256 timer.cancel()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
257
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
258 if upload_request.filename != filename:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
259 log.warning(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
260 f"invalid filename for PUT (upload id: {upload_id!r}, URL: {self.channel._path.decode()}). Original "
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
261 f"{upload_request.filename!r} doesn't match {filename!r}"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
262 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
263 return self.refuseRequest()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
264
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
265 self.upload_request_data = (client, upload_request)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
266
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
267 file_tmp_path = files_utils.get_unique_name(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
268 self.file_tmp_dir/upload_id)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
269
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
270 self.content = open(file_tmp_path, 'w+b')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
271 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
272 return super().gotLength(length)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
273
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
274
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
275 class FileSharingSite(server.Site):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
276 requestFactory = FileSharingRequest
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
277
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
278 def __init__(self, file_sharing):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
279 self.file_sharing = file_sharing
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
280 self.file_tmp_dir = file_sharing.host.getLocalPath(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
281 None, C.FILES_TMP_DIR, TMP_BUFFER_DIR, component=True, profile=False
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
282 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
283 for old_file in self.file_tmp_dir.iterdir():
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
284 log.debug(f"purging old buffer file at {old_file}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
285 old_file.unlink()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
286 super().__init__(HTTPFileServer())
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
287
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
288 def getContentFile(self, length):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
289 file_tmp_path = self.file_tmp_dir/shortuuid.uuid()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
290 return open(file_tmp_path, 'w+b')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
291
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
292
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
293 class FileSharing:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
294
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
295 def __init__(self, host):
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
296 log.info(_("File Sharing initialization"))
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
297 self.host = host
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
298 self._f = host.plugins["FILE"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
299 self._jf = host.plugins["XEP-0234"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
300 self._h = host.plugins["XEP-0300"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
301 self._t = host.plugins["XEP-0264"]
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
302 host.plugins["XEP-0363"].registerHandler(self._onHTTPUpload)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
303 host.trigger.add("FILE_getDestDir", self._getDestDirTrigger)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
304 host.trigger.add(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
305 "XEP-0234_fileSendingRequest", self._fileSendingRequestTrigger, priority=1000
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
306 )
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
307 host.trigger.add("XEP-0234_buildFileElement", self._addFileComments)
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
308 host.trigger.add("XEP-0234_parseFileElement", self._getFileComments)
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
309 host.trigger.add("XEP-0329_compGetFilesFromNode", self._addCommentsData)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
310 self.files_path = host.getLocalPath(None, C.FILES_DIR, profile=False)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
311 self.http_port = host.memory.getConfig(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
312 'component file_sharing', 'http_upload_port', 8888)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
313 connection_type = host.memory.getConfig(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
314 'component file_sharing', 'http_upload_connection_type', 'https')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
315 if connection_type not in ('http', 'https'):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
316 raise exceptions.ConfigError(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
317 f'bad http_upload_connection_type, you must use one of "http" or "https"'
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
318 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
319 self.server = FileSharingSite(self)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
320 self.expected_uploads = {}
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
321 if connection_type == 'http':
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
322 reactor.listenTCP(self.http_port, self.server)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
323 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
324 options = tls.getOptionsFromConfig(host.memory.config, "component file_sharing")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
325 tls.TLSOptionsCheck(options)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
326 context_factory = tls.getTLSContextFactory(options)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
327 reactor.listenSSL(self.http_port, self.server, context_factory)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
328
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
329
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
330 def getHandler(self, client):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
331 return Comments_handler(self)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
332
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
333 def profileConnecting(self, client):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
334 public_base_url = self.host.memory.getConfig(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
335 'component file_sharing', 'http_upload_public_facing_url')
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
336 if public_base_url is None:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
337 client._file_sharing_base_url = f"https://{client.host}:{self.http_port}"
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
338 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
339 client._file_sharing_base_url = public_base_url
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
340 client._file_sharing_allowed_hosts = self.host.memory.getConfig(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
341 'component file_sharing', 'http_upload_allowed_hosts_list') or [client.host]
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
342 path = client.file_tmp_dir = os.path.join(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
343 self.host.memory.getConfig("", "local_dir"),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
344 C.FILES_TMP_DIR,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
345 regex.pathEscape(client.profile),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
346 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 if not os.path.exists(path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
348 os.makedirs(path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
349
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
350 async def registerReceivedFile(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
351 self, client, peer_jid, file_data, file_path, public_id=None, extra=None):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
352 """Post file reception tasks
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
353
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
354 once file is received, this method create hash/thumbnails if necessary
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
355 move the file to the right location, and create metadata entry in database
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
356 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
357 name = file_data["name"]
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
358 if extra is None:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
359 extra = {}
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
360
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
361 if file_data.get("hash_algo") == HASH_ALGO:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
362 log.debug(_("Reusing already generated hash"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
363 file_hash = file_data["hash_hasher"].hexdigest()
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
364 else:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
365 hasher = self._h.getHasher(HASH_ALGO)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
366 with file_path.open('rb') as f:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
367 file_hash = await self._h.calculateHash(f, hasher)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
368 final_path = self.files_path/file_hash
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
369
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
370 if final_path.is_file():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
371 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
372 "file [{file_hash}] already exists, we can remove temporary one".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
373 file_hash=file_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
375 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
376 file_path.unlink()
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
377 else:
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
378 file_path.rename(final_path)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
379 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
380 "file [{file_hash}] moved to {files_path}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
381 file_hash=file_hash, files_path=self.files_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
382 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
383 )
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
384
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
385 mime_type = file_data.get("mime_type")
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
386 if not mime_type or mime_type == "application/octet-stream":
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
387 mime_type = mimetypes.guess_type(name)[0]
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
388
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
389 if mime_type is not None and mime_type.startswith("image"):
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
390 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
391 for max_thumb_size in (self._t.SIZE_SMALL, self._t.SIZE_MEDIUM):
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
392 try:
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
393 thumb_size, thumb_id = await self._t.generateThumbnail(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
394 final_path,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
395 max_thumb_size,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
396 #  we keep thumbnails for 6 months
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
397 60 * 60 * 24 * 31 * 6,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
398 )
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
399 except Exception as e:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
400 log.warning(_("Can't create thumbnail: {reason}").format(reason=e))
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
401 break
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
402 thumbnails.append({"id": thumb_id, "size": thumb_size})
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
403
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
404 self.host.memory.setFile(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
405 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
406 name=name,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
407 version="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
408 file_hash=file_hash,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
409 hash_algo=HASH_ALGO,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
410 size=file_data["size"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
411 path=file_data.get("path"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
412 namespace=file_data.get("namespace"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
413 mime_type=mime_type,
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
414 public_id=public_id,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
415 owner=peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
416 extra=extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
417 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
418
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
419 def _getDestDirTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
420 self, client, peer_jid, transfer_data, file_data, stream_object
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
421 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
422 """This trigger accept file sending request, and store file locally"""
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
423 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
424 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
425 assert stream_object
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
426 assert "stream_object" not in transfer_data
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
427 assert C.KEY_PROGRESS_ID in file_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
428 filename = file_data["name"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
429 assert filename and not "/" in filename
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
430 file_tmp_dir = self.host.getLocalPath(
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
431 None, C.FILES_TMP_DIR, peer_jid.userhost(), component=True, profile=False
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
432 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
433 file_tmp_path = file_data['file_path'] = files_utils.get_unique_name(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
434 file_tmp_dir/filename)
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
435
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
436 transfer_data["finished_d"].addCallback(
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
437 lambda __: defer.ensureDeferred(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
438 self.registerReceivedFile(client, peer_jid, file_data, file_tmp_path)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
439 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
440 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
441
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
442 self._f.openFileWrite(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
443 client, file_tmp_path, transfer_data, file_data, stream_object
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
444 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
445 return False, defer.succeed(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
446
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
447 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
448 def _retrieveFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
449 self, client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
450 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
451 """This method retrieve a file on request, and send if after checking permissions"""
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
452 peer_jid = session["peer_jid"]
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
453 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
454 found_files = yield self.host.memory.getFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
455 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
456 peer_jid=peer_jid,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
457 name=file_data.get("name"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
458 file_hash=file_data.get("file_hash"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
459 hash_algo=file_data.get("hash_algo"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
460 path=file_data.get("path"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
461 namespace=file_data.get("namespace"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
462 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
463 except exceptions.NotFound:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
464 found_files = None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
465 except exceptions.PermissionError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
466 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
467 _("{peer_jid} is trying to access an unauthorized file: {name}").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
468 peer_jid=peer_jid, name=file_data.get("name")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
469 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
470 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
471 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
472
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
473 if not found_files:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
474 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
475 _("no matching file found ({file_data})").format(file_data=file_data)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
476 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
477 defer.returnValue(False)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
478
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
479 # we only use the first found file
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
480 found_file = found_files[0]
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
481 if found_file['type'] != C.FILE_TYPE_FILE:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
482 raise TypeError("a file was expected, type is {type_}".format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
483 type_=found_file['type']))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
484 file_hash = found_file["file_hash"]
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
485 file_path = os.path.join(self.files_path, file_hash)
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
486 file_data["hash_hasher"] = hasher = self._h.getHasher(found_file["hash_algo"])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
487 size = file_data["size"] = found_file["size"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
488 file_data["file_hash"] = file_hash
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
489 file_data["hash_algo"] = found_file["hash_algo"]
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
490
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
491 # we complete file_elt so peer can have some details on the file
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
492 if "name" not in file_data:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
493 file_elt.addElement("name", content=found_file["name"])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
494 file_elt.addElement("size", content=str(size))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
495 content_data["stream_object"] = stream.FileStreamObject(
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
496 self.host,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
497 client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
498 file_path,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
499 uid=self._jf.getProgressId(session, content_name),
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
500 size=size,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
501 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
502 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
503 defer.returnValue(True)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
504
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
505 def _fileSendingRequestTrigger(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
506 self, client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
507 ):
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
508 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
509 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
510 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
511 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
512 False,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
513 self._retrieveFiles(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
514 client, session, content_data, content_name, file_data, file_elt
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
515 ),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
516 )
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
517
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
518 ## HTTP Upload ##
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
519
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
520 def _purge_slot(self, upload_id):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
521 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
522 del self.expected_uploads[upload_id]
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
523 except KeyError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
524 log.error(f"trying to purge an inexisting upload slot ({upload_id})")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
525
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
526 def _onHTTPUpload(self, client, request):
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
527 # filename should be already cleaned, but it's better to double check
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
528 assert '/' not in request.filename
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
529 if request.from_.host not in client._file_sharing_allowed_hosts:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
530 raise error.StanzaError("forbidden")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
531
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
532 upload_id = shortuuid.ShortUUID().random(length=30)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
533 assert '/' not in upload_id
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
534 timer = reactor.callLater(30, self._purge_slot, upload_id)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
535 self.expected_uploads[upload_id] = (client, request, timer)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
536 url = urljoin(client._file_sharing_base_url, f"{upload_id}/{request.filename}")
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
537 slot = self.host.plugins["XEP-0363"].Slot(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
538 put=url,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
539 get=url,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
540 headers=[],
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
541 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
542 return slot
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
543
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
544 ## comments triggers ##
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
545
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
546 def _addFileComments(self, file_elt, extra_args):
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
547 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
548 comments_url = extra_args.pop("comments_url")
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
549 except KeyError:
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
550 return
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
551
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
552 comment_elt = file_elt.addElement((NS_COMMENTS, "comments"), content=comments_url)
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
553
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
554 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
555 count = len(extra_args["extra"]["comments"])
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
556 except KeyError:
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
557 count = 0
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
558
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
559 comment_elt["count"] = str(count)
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
560 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
561
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
562 def _getFileComments(self, file_elt, file_data):
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
563 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
564 comments_elt = next(file_elt.elements(NS_COMMENTS, "comments"))
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
565 except StopIteration:
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
566 return
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
567 file_data["comments_url"] = str(comments_elt)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
568 file_data["comments_count"] = comments_elt["count"]
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
569 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
570
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
571 def _addCommentsData(self, client, iq_elt, owner, node_path, files_data):
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
572 for file_data in files_data:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
573 file_data["comments_url"] = uri.buildXMPPUri(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
574 "pubsub",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
575 path=client.jid.full(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
576 node=COMMENT_NODE_PREFIX + file_data["id"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
577 )
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
578 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
579
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
580
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
581 class Comments_handler(pubsub.PubSubService):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
582 """This class is a minimal Pubsub service handling virtual nodes for comments"""
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
583
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
584 def __init__(self, plugin_parent):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
585 super(Comments_handler, self).__init__() # PubsubVirtualResource())
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
586 self.host = plugin_parent.host
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
587 self.plugin_parent = plugin_parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
588 self.discoIdentity = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
589 "category": "pubsub",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
590 "type": "virtual", # FIXME: non standard, here to avoid this service being considered as main pubsub one
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
591 "name": "files commenting service",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
592 }
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
593
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
594 def _getFileId(self, nodeIdentifier):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
595 if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
596 raise error.StanzaError("item-not-found")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
597 file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX) :]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
598 if not file_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
599 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
600 return file_id
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
601
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
602 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
603 def getFileData(self, requestor, nodeIdentifier):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
604 file_id = self._getFileId(nodeIdentifier)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
605 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
606 files = yield self.host.memory.getFiles(self.parent, requestor, file_id)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
607 except (exceptions.NotFound, exceptions.PermissionError):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
608 # we don't differenciate between NotFound and PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
609 # to avoid leaking information on existing files
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
610 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
611 if not files:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
612 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
613 if len(files) > 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
614 raise error.InternalError("there should be only one file")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
615 defer.returnValue(files[0])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
616
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
617 def commentsUpdate(self, extra, new_comments, peer_jid):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
618 """update comments (replace or insert new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
619
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
620 @param extra(dict): extra data to update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
621 @param new_comments(list[tuple(unicode, unicode, unicode)]): comments to update or insert
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
622 @param peer_jid(unicode, None): bare jid of the requestor, or None if request is done by owner
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
623 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
624 current_comments = extra.setdefault("comments", [])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
625 new_comments_by_id = {c[0]: c for c in new_comments}
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
626 updated = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
627 # we now check every current comment, to see if one id in new ones
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
628 # exist, in which case we must update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
629 for idx, comment in enumerate(current_comments):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
630 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
631 if comment_id in new_comments_by_id:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
632 # a new comment has an existing id, update is requested
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
633 if peer_jid and comment[1] != peer_jid:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
634 # requestor has not the right to modify the comment
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
635 raise exceptions.PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
636 # we replace old_comment with updated one
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
637 new_comment = new_comments_by_id[comment_id]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
638 current_comments[idx] = new_comment
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
639 updated.append(new_comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
640
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
641 # we now remove every updated comments, to only keep
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
642 # the ones to insert
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
643 for comment in updated:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
644 new_comments.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
645
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
646 current_comments.extend(new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
647
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
648 def commentsDelete(self, extra, comments):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
649 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
650 comments_dict = extra["comments"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
651 except KeyError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
652 return
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
653 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
654 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
655 comments_dict.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
656 except ValueError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
657 continue
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
658
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
659 def _getFrom(self, item_elt):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
660 """retrieve published of an item
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
661
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
662 @param item_elt(domish.element): <item> element
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
663 @return (unicode): full jid as string
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
664 """
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
665 iq_elt = item_elt
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
666 while iq_elt.parent != None:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
667 iq_elt = iq_elt.parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
668 return iq_elt["from"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
669
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
670 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
671 def publish(self, requestor, service, nodeIdentifier, items):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
672 #  we retrieve file a first time to check authorisations
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
673 file_data = yield self.getFileData(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
674 file_id = file_data["id"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
675 comments = [(item["id"], self._getFrom(item), item.toXml()) for item in items]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
676 if requestor.userhostJID() == file_data["owner"]:
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
677 peer_jid = None
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
678 else:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
679 peer_jid = requestor.userhost()
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
680 update_cb = partial(self.commentsUpdate, new_comments=comments, peer_jid=peer_jid)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
681 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
682 yield self.host.memory.fileUpdate(file_id, "extra", update_cb)
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
683 except exceptions.PermissionError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
684 raise error.StanzaError("not-authorized")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
685
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
686 @defer.inlineCallbacks
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
687 def items(self, requestor, service, nodeIdentifier, maxItems, itemIdentifiers):
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
688 file_data = yield self.getFileData(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
689 comments = file_data["extra"].get("comments", [])
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
690 if itemIdentifiers:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
691 defer.returnValue(
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
692 [generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
693 )
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
694 else:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
695 defer.returnValue([generic.parseXml(c[2]) for c in comments])
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
696
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
697 @defer.inlineCallbacks
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
698 def retract(self, requestor, service, nodeIdentifier, itemIdentifiers):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
699 file_data = yield self.getFileData(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
700 file_id = file_data["id"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
701 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
702 comments = file_data["extra"]["comments"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
703 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
704 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
705
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
706 to_remove = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
707 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
708 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
709 if comment_id in itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
710 to_remove.append(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
711 itemIdentifiers.remove(comment_id)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
712 if not itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
713 break
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
714
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
715 if itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
716 # not all items have been to_remove, we can't continue
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
717 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
718
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
719 if requestor.userhostJID() != file_data["owner"]:
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
720 if not all([c[1] == requestor.userhost() for c in to_remove]):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
721 raise error.StanzaError("not-authorized")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
722
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
723 remove_cb = partial(self.commentsDelete, comments=to_remove)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
724 yield self.host.memory.fileUpdate(file_id, "extra", remove_cb)