annotate libervia/backend/plugins/plugin_comp_file_sharing.py @ 4309:b56b1eae7994

component email gateway: add multicasting: XEP-0033 multicasting is now supported both for incoming and outgoing messages. XEP-0033 metadata are converted to suitable Email headers and vice versa. Email address and JID are both supported, and delivery is done by the gateway when suitable on incoming messages. rel 450
author Goffi <goffi@goffi.org>
date Thu, 26 Sep 2024 16:12:01 +0200
parents 0d7bb4df2343
children
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
3677
02e5e2385a30 component (file-sharing): use `file-sharing` instead of `file_sharing`:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
3 # Libervia File Sharing component
3479
be6d91572633 date update
Goffi <goffi@goffi.org>
parents: 3367
diff changeset
4 # Copyright (C) 2009-2021 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
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
22 import tempfile
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
23 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
24 import shortuuid
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
25 import unicodedata
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
26 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
27 from pathlib import Path
4071
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
28 from libervia.backend.core.i18n import _, D_
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
29 from libervia.backend.core.constants import Const as C
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
30 from libervia.backend.core import exceptions
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
31 from libervia.backend.core.log import getLogger
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
32 from libervia.backend.tools import stream
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
33 from libervia.backend.tools import video
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
34 from libervia.backend.tools.utils import ensure_deferred
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
35 from libervia.backend.tools.common import regex
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
36 from libervia.backend.tools.common import uri
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
37 from libervia.backend.tools.common import files_utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
38 from libervia.backend.tools.common import utils
4b842c1fb686 refactoring: renamed `sat` package to `libervia.backend`
Goffi <goffi@goffi.org>
parents: 4037
diff changeset
39 from libervia.backend.tools.common import tls
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
40 from twisted.internet import defer, reactor
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
41 from twisted.words.protocols.jabber import error
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
42 from twisted.web import server, resource, static, http
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
43 from wokkel import pubsub
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
44 from wokkel import generic
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
45
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
46
3136
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
47 log = getLogger(__name__)
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
48
9d0df638c8b4 dates update
Goffi <goffi@goffi.org>
parents: 3028
diff changeset
49
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
50 PLUGIN_INFO = {
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
51 C.PI_NAME: "File sharing component",
3677
02e5e2385a30 component (file-sharing): use `file-sharing` instead of `file_sharing`:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
52 C.PI_IMPORT_NAME: "file-sharing",
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
53 C.PI_MODES: [C.PLUG_MODE_COMPONENT],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
54 C.PI_TYPE: C.PLUG_TYPE_ENTRY_POINT,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
55 C.PI_PROTOCOLS: [],
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
56 C.PI_DEPENDENCIES: [
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
57 "FILE",
2929
e0429ff7f6b6 plugin comp file sharing: file sharing management first draft:
Goffi <goffi@goffi.org>
parents: 2771
diff changeset
58 "FILE_SHARING_MANAGEMENT",
3367
90aee1f2d07c component file sharing: added missing `owner` filtering in `_retrieveFiles`
Goffi <goffi@goffi.org>
parents: 3363
diff changeset
59 "XEP-0106",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
60 "XEP-0234",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
61 "XEP-0260",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
62 "XEP-0261",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
63 "XEP-0264",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
64 "XEP-0329",
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
65 "XEP-0363",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
66 ],
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
67 C.PI_RECOMMENDATIONS: [],
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
68 C.PI_MAIN: "FileSharing",
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
69 C.PI_HANDLER: C.BOOL_TRUE,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
70 C.PI_DESCRIPTION: _("""Component hosting and sharing files"""),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
71 }
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
72
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
73 HASH_ALGO = "sha-256"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
74 NS_COMMENTS = "org.salut-a-toi.comments"
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
75 NS_FS_AFFILIATION = "org.salut-a-toi.file-sharing-affiliation"
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
76 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
77 # 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
78 # 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
79 TMP_BUFFER_DIR = "@@tmp@@"
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
80 OVER_QUOTA_TXT = D_(
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
81 "You are over quota, your maximum allowed size is {quota} and you are already using "
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
82 "{used_space}, you can't upload {file_size} more."
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
83 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
84
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
85 HTTP_VERSION = unicodedata.normalize("NFKD", f"{C.APP_NAME} file sharing {C.APP_VERSION}")
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
86
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
87
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
88 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
89 isLeaf = True
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
90
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
91 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
92 request.setResponseCode(code)
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
93 if code == http.BAD_REQUEST:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
94 brief = "Bad Request"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
95 details = "Your request is invalid"
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
96 elif code == http.FORBIDDEN:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
97 brief = "Forbidden"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
98 details = "You're not allowed to use this resource"
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
99 elif code == http.NOT_FOUND:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
100 brief = "Not Found"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
101 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
102 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
103 brief = "Error"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
104 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
105 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
106 log.warning(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
107 f"Error returned while trying to access url {request.uri.decode()}: "
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
108 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
109 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
110
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
111 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
112
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
113 def get_disposition_type(self, media_type, media_subtype):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
114 if media_type in ("image", "video"):
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
115 return "inline"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
116 elif media_type == "application" and media_subtype == "pdf":
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
117 return "inline"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
118 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
119 return "attachment"
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
120
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
121 def render(self, request):
3722
75f8d206e551 component file sharing: set server name at render time
Goffi <goffi@goffi.org>
parents: 3721
diff changeset
122 request.setHeader("server", HTTP_VERSION)
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
123 request.setHeader("Access-Control-Allow-Origin", "*")
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
124 request.setHeader("Access-Control-Allow-Methods", "OPTIONS, HEAD, GET, PUT")
3340
2ad14b834730 component file sharing: added `Access-Control-Expose-Headers` CORS header:
Goffi <goffi@goffi.org>
parents: 3331
diff changeset
125 request.setHeader(
2ad14b834730 component file sharing: added `Access-Control-Expose-Headers` CORS header:
Goffi <goffi@goffi.org>
parents: 3331
diff changeset
126 "Access-Control-Allow-Headers",
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
127 "Content-Type, Range, Xmpp-File-Path, Xmpp-File-No-Http",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
128 )
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
129 request.setHeader("Access-Control-Allow-Credentials", "true")
3340
2ad14b834730 component file sharing: added `Access-Control-Expose-Headers` CORS header:
Goffi <goffi@goffi.org>
parents: 3331
diff changeset
130 request.setHeader("Accept-Ranges", "bytes")
2ad14b834730 component file sharing: added `Access-Control-Expose-Headers` CORS header:
Goffi <goffi@goffi.org>
parents: 3331
diff changeset
131
2ad14b834730 component file sharing: added `Access-Control-Expose-Headers` CORS header:
Goffi <goffi@goffi.org>
parents: 3331
diff changeset
132 request.setHeader(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
133 "Access-Control-Expose-Headers", "Date, Content-Length, Content-Range"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
134 )
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
135 return super().render(request)
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
136
4097
0f6fd28fde0d component file sharing: fix method name case:
Goffi <goffi@goffi.org>
parents: 4071
diff changeset
137 def render_OPTIONS(self, request):
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
138 request.setResponseCode(http.OK)
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
139 return b""
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
140
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
141 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
142 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
143 request.upload_data
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
144 except exceptions.DataError:
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
145 return self.errorPage(request, http.NOT_FOUND)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
146
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
147 defer.ensureDeferred(self.render_get(request))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
148 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
149
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
150 async def render_get(self, request):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
151 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
152 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
153 except exceptions.DataError:
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
154 request.write(self.errorPage(request, http.FORBIDDEN))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
155 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
156 return
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
157 found_files = await request.file_sharing.host.memory.get_files(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
158 client=None, peer_jid=None, perms_to_check=None, public_id=upload_id
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
159 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
160 if not found_files:
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
161 request.write(self.errorPage(request, http.NOT_FOUND))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
162 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
163 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
164 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
165 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
166
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
167 found_file = found_files[0]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
168 file_path = request.file_sharing.files_path / found_file["file_hash"]
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
169 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
170 file_res.type = f'{found_file["media_type"]}/{found_file["media_subtype"]}'
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
171 file_res.encoding = file_res.contentEncodings.get(Path(found_file["name"]).suffix)
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
172 disp_type = self.get_disposition_type(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
173 found_file["media_type"], found_file["media_subtype"]
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
174 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
175 # 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
176 # 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
177 request.setHeader(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
178 "Content-Disposition",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
179 f"{disp_type}; filename*=UTF-8''{quote(found_file['name'])}",
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
180 )
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
181 # cf. https://xmpp.org/extensions/xep-0363.html#server
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
182 request.setHeader(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
183 "Content-Security-Policy", "default-src 'none'; frame-ancestors 'none';"
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
184 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
185 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
186 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
187 # 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
188 request.write(ret)
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
189 request.finish()
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 def render_PUT(self, request):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
192 defer.ensureDeferred(self.render_put(request))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
193 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
194
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
195 async def render_put(self, request):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
196 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
197 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
198 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
199 except AttributeError:
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
200 request.write(self.errorPage(request, http.BAD_REQUEST))
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
201 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
202 return
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
203
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
204 # 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
205 # 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
206 request.content.close()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
207 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
208 request.content = None
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
209
3297
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
210 # the 2 following headers are not standard, but useful in the context of file
3677
02e5e2385a30 component (file-sharing): use `file-sharing` instead of `file_sharing`:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
211 # sharing with HTTP Upload: first one allow uploader to specify the path
3297
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
212 # and second one will disable public exposure of the file through HTTP
3301
9d1c0feba048 comp file sharing: file paths are now quoted on upload to allow unicode
Goffi <goffi@goffi.org>
parents: 3297
diff changeset
213 path = request.getHeader("Xmpp-File-Path")
9d1c0feba048 comp file sharing: file paths are now quoted on upload to allow unicode
Goffi <goffi@goffi.org>
parents: 3297
diff changeset
214 if path:
9d1c0feba048 comp file sharing: file paths are now quoted on upload to allow unicode
Goffi <goffi@goffi.org>
parents: 3297
diff changeset
215 path = unquote(path)
9d1c0feba048 comp file sharing: file paths are now quoted on upload to allow unicode
Goffi <goffi@goffi.org>
parents: 3297
diff changeset
216 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
217 path = "/uploads"
3297
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
218 if request.getHeader("Xmpp-File-No-Http") is not None:
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
219 public_id = None
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
220 else:
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
221 public_id = upload_id
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
222
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
223 file_data = {
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
224 "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
225 "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
226 "size": upload_request.size,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
227 "path": path,
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
228 }
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
229
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
230 await request.file_sharing.register_received_file(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
231 client,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
232 upload_request.from_,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
233 file_data,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
234 tmp_file_path,
3297
91b5ae058c66 comp file sharing: handle upload with headers:
Goffi <goffi@goffi.org>
parents: 3296
diff changeset
235 public_id=public_id,
3289
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
3296
da443cf946ad comp file sharing: CORS:
Goffi <goffi@goffi.org>
parents: 3291
diff changeset
238 request.setResponseCode(http.CREATED)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
239 request.finish()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
240
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
241
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
242 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
243
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
244 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
245 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
246 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
247
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
248 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
249 def upload_data(self):
3677
02e5e2385a30 component (file-sharing): use `file-sharing` instead of `file_sharing`:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
250 """A tuple with upload_id and filename retrieved from requested path"""
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
251 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
252 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
253
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
254 # self.path is not available if we are early in the request (e.g. when gotLength
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
255 # 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
256 # 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
257 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
258 # 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
259 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
260 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
261 __, upload_id, filename = path.split("/")
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
262 except ValueError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
263 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
264 if len(upload_id) < 10:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
265 raise exceptions.DataError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
266 f"invalid upload ID received for a PUT: {upload_id!r}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
267 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
268
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
269 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
270 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
271
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
272 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
273 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
274 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
275
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
276 @property
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
277 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
278 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
279
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
280 def refuse_request(self):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
281 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
282 self.content.close()
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
283 self.content = open(os.devnull, "w+b")
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
284 self.channel._respondToBadRequestAndDisconnect()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
285
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
286 def gotLength(self, length):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
287 if self.channel._command.decode().upper() == "PUT":
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
288 # 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
289 # 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
290 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
291 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
292 except exceptions.DataError as e:
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
293 log.warning(f"Invalid PUT request, we stop here: {e}")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
294 return self.refuse_request()
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
295 try:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
296 client, upload_request, timer = self.file_sharing.expected_uploads.pop(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
297 upload_id
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
298 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
299 except KeyError:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
300 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
301 f"unknown (expired?) upload ID received for a PUT: {upload_id!r}"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
302 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
303 return self.refuse_request()
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
304
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
305 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
306 log.warning(f"upload id {upload_id!r} used for a PUT, but it is expired")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
307 return self.refuse_request()
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
308
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
309 timer.cancel()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
310
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
311 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
312 log.warning(
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
313 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
314 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
315 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
316 return self.refuse_request()
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
317
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
318 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
319
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
320 file_tmp_path = files_utils.get_unique_name(self.file_tmp_dir / upload_id)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
321
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
322 self.content = open(file_tmp_path, "w+b")
3289
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 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
325
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
326
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
327 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
328 requestFactory = FileSharingRequest
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
329
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
330 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
331 self.file_sharing = file_sharing
3932
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3859
diff changeset
332 self.file_tmp_dir = file_sharing.host.get_local_path(
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3859
diff changeset
333 None, C.FILES_TMP_DIR, TMP_BUFFER_DIR, component=True
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
334 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
335 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
336 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
337 old_file.unlink()
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
338 super().__init__(HTTPFileServer())
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
339
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
340 def getContentFile(self, length):
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
341 file_tmp_path = self.file_tmp_dir / shortuuid.uuid()
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
342 return open(file_tmp_path, "w+b")
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
343
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
344
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
345 class FileSharing:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
346
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
347 def __init__(self, host):
3291
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
348 self.host = host
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
349 self.initialised = False
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
350
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
351 def init(self):
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
352 # we init once on first component connection,
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
353 # there is not need to init this plugin if not component use it
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
354 # TODO: this plugin should not be loaded at all if no component uses it
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
355 # and should be loaded dynamically as soon as a suitable profile is created
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
356 if self.initialised:
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
357 return
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
358 self.initialised = True
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
359 log.info(_("File Sharing initialization"))
3291
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
360 self._f = self.host.plugins["FILE"]
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
361 self._jf = self.host.plugins["XEP-0234"]
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
362 self._h = self.host.plugins["XEP-0300"]
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
363 self._t = self.host.plugins["XEP-0264"]
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
364 self._hu = self.host.plugins["XEP-0363"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
365 self._hu.register_handler(self._on_http_upload)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
366 self.host.trigger.add_with_check(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
367 "FILE_getDestDir", self, self._get_dest_dir_trigger
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
368 )
4183
6784d07b99c8 plugin XEP-053, component AP gateway: use the new `trigger.add_with_check` method
Goffi <goffi@goffi.org>
parents: 4097
diff changeset
369 self.host.trigger.add_with_check(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
370 "XEP-0234_fileSendingRequest",
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
371 self,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
372 self._file_sending_request_trigger,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
373 priority=1000,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
374 )
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
375 self.host.trigger.add_with_check(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
376 "XEP-0234_buildFileElement", self, self._add_file_metadata_elts
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
377 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
378 self.host.trigger.add_with_check(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
379 "XEP-0234_parseFileElement", self, self._get_file_metadata_elts
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
380 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
381 self.host.trigger.add_with_check(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
382 "XEP-0329_compGetFilesFromNode", self, self._add_file_metadata
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
383 )
4183
6784d07b99c8 plugin XEP-053, component AP gateway: use the new `trigger.add_with_check` method
Goffi <goffi@goffi.org>
parents: 4097
diff changeset
384 self.host.trigger.add_with_check(
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
385 "XEP-0329_compGetFilesFromNode_build_directory",
4183
6784d07b99c8 plugin XEP-053, component AP gateway: use the new `trigger.add_with_check` method
Goffi <goffi@goffi.org>
parents: 4097
diff changeset
386 self,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
387 self._add_directory_metadata_elts,
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
388 )
4183
6784d07b99c8 plugin XEP-053, component AP gateway: use the new `trigger.add_with_check` method
Goffi <goffi@goffi.org>
parents: 4097
diff changeset
389 self.host.trigger.add_with_check(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
390 "XEP-0329_parseResult_directory", self, self._get_directory_metadata_elts
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
391 )
3932
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3859
diff changeset
392 self.files_path = self.host.get_local_path(None, C.FILES_DIR)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
393 self.http_port = int(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
394 self.host.memory.config_get(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
395 "component file-sharing", "http_upload_port", 8888
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
396 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
397 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
398 connection_type = self.host.memory.config_get(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
399 "component file-sharing", "http_upload_connection_type", "https"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
400 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
401 if connection_type not in ("http", "https"):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
402 raise exceptions.ConfigError(
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
403 'bad http_upload_connection_type, you must use one of "http" or "https"'
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
404 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
405 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
406 self.expected_uploads = {}
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
407 if connection_type == "http":
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
408 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
409 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
410 options = tls.get_options_from_config(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
411 self.host.memory.config, "component file-sharing"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
412 )
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
413 tls.tls_options_check(options)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
414 context_factory = tls.get_tls_context_factory(options)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
415 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
416
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
417 def get_handler(self, client):
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
418 return Comments_handler(self)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
419
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
420 def profile_connecting(self, client):
4021
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
421 # we activate HTTP upload
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
422 client.enabled_features.add("XEP-0363")
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
423
3291
449dfbfcdbcc component file sharing: don't initialise the plugin if not component use it
Goffi <goffi@goffi.org>
parents: 3289
diff changeset
424 self.init()
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
425 public_base_url = self.host.memory.config_get(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
426 "component file-sharing", "http_upload_public_facing_url"
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
427 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
428 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
429 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
430 else:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
431 client._file_sharing_base_url = public_base_url
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
432 path = client.file_tmp_dir = os.path.join(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
433 self.host.memory.config_get("", "local_dir"),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
434 C.FILES_TMP_DIR,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
435 regex.path_escape(client.profile),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
436 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
437 if not os.path.exists(path):
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
438 os.makedirs(path)
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
439
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
440 def get_quota(self, client, entity):
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
441 """Return maximum size allowed for all files for entity"""
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
442 quotas = self.host.memory.config_get("component file-sharing", "quotas_json", {})
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
443 if self.host.memory.is_admin_jid(entity):
3778
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
444 quota = quotas.get("admins")
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
445 else:
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
446 try:
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
447 quota = quotas["jids"][entity.userhost()]
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
448 except KeyError:
7b90d7c474fa comp file sharing: handle `admins` in quotas
Goffi <goffi@goffi.org>
parents: 3722
diff changeset
449 quota = quotas.get("users")
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
450 return None if quota is None else utils.parse_size(quota)
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
451
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
452 async def generate_thumbnails(self, extra: dict, image_path: Path):
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
453 thumbnails = extra.setdefault(C.KEY_THUMBNAILS, [])
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
454 for max_thumb_size in self._t.SIZES:
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
455 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
456 thumb_size, thumb_id = await self._t.generate_thumbnail(
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
457 image_path,
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
458 max_thumb_size,
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
459 #  we keep thumbnails for 6 months
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
460 60 * 60 * 24 * 31 * 6,
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
461 )
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
462 except Exception as e:
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
463 log.warning(_("Can't create thumbnail: {reason}").format(reason=e))
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
464 break
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
465 thumbnails.append({"id": thumb_id, "size": thumb_size})
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
466
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
467 async def register_received_file(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
468 self, client, peer_jid, file_data, file_path, public_id=None, extra=None
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
469 ):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
470 """Post file reception tasks
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
471
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
472 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
473 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
474 """
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
475 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
476 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
477 extra = {}
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
478
3331
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
479 mime_type = file_data.get("mime_type")
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
480 if not mime_type or mime_type == "application/octet-stream":
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
481 mime_type = mimetypes.guess_type(name)[0]
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
482
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
483 is_image = mime_type is not None and mime_type.startswith("image")
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
484 is_video = mime_type is not None and mime_type.startswith("video")
3331
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
485
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
486 if file_data.get("hash_algo") == HASH_ALGO:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
487 log.debug(_("Reusing already generated hash"))
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
488 file_hash = file_data["hash_hasher"].hexdigest()
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
489 else:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
490 hasher = self._h.get_hasher(HASH_ALGO)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
491 with file_path.open("rb") as f:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
492 file_hash = await self._h.calculate_hash(f, hasher)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
493 final_path = self.files_path / file_hash
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
494
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
495 if final_path.is_file():
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
496 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
497 "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
498 file_hash=file_hash
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
499 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
500 )
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
501 file_path.unlink()
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
502 else:
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
503 file_path.rename(final_path)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
504 log.debug(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
505 "file [{file_hash}] moved to {files_path}".format(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
506 file_hash=file_hash, files_path=self.files_path
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
507 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
508 )
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
509
3331
b1e9f17fbb5a comp file sharing: minor core re-organisation
Goffi <goffi@goffi.org>
parents: 3329
diff changeset
510 if is_image:
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
511 await self.generate_thumbnails(extra, final_path)
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
512 elif is_video:
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
513 with tempfile.TemporaryDirectory() as tmp_dir:
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
514 thumb_path = Path(tmp_dir) / "thumbnail.jpg"
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
515 try:
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
516 await video.get_thumbnail(final_path, thumb_path)
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
517 except Exception as e:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
518 log.warning(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
519 _("Can't get thumbnail for {final_path}: {e}").format(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
520 final_path=final_path, e=e
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
521 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
522 )
3348
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
523 else:
12c427156cac component file sharing: generate thumbnails for videos
Goffi <goffi@goffi.org>
parents: 3340
diff changeset
524 await self.generate_thumbnails(extra, thumb_path)
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
525
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
526 await self.host.memory.set_file(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
527 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
528 name=name,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
529 version="",
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
530 file_hash=file_hash,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
531 hash_algo=HASH_ALGO,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
532 size=file_data["size"],
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
533 path=file_data.get("path"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
534 namespace=file_data.get("namespace"),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
535 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
536 public_id=public_id,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
537 owner=peer_jid,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
538 extra=extra,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
539 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
540
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
541 async def _get_dest_dir_trigger(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
542 self, client, peer_jid, transfer_data, file_data, stream_object
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
543 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
544 """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
545 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
546 return True, None
3532
ab72b8ac3bd2 component file sharing: forbid jingle upload by entities which are not in allowed hosts
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
547 # client._file_sharing_allowed_hosts is set in plugin XEP-0329
ab72b8ac3bd2 component file sharing: forbid jingle upload by entities which are not in allowed hosts
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
548 if peer_jid.host not in client._file_sharing_allowed_hosts:
ab72b8ac3bd2 component file sharing: forbid jingle upload by entities which are not in allowed hosts
Goffi <goffi@goffi.org>
parents: 3528
diff changeset
549 raise error.StanzaError("forbidden")
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
550 assert stream_object
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
551 assert "stream_object" not in transfer_data
2514
4440ea7047bd file sharing component: thumbnails integration first draft:
Goffi <goffi@goffi.org>
parents: 2504
diff changeset
552 assert C.KEY_PROGRESS_ID in file_data
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
553 filename = file_data["name"]
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
554 assert filename and not "/" in filename
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
555 quota = self.get_quota(client, peer_jid)
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
556 if quota is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
557 used_space = await self.host.memory.file_get_used_space(client, peer_jid)
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
558
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
559 if (used_space + file_data["size"]) > quota:
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
560 raise error.StanzaError(
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
561 "not-acceptable",
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
562 text=OVER_QUOTA_TXT.format(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
563 quota=utils.get_human_size(quota),
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
564 used_space=utils.get_human_size(used_space),
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
565 file_size=utils.get_human_size(file_data["size"]),
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
566 ),
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
567 )
3932
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3859
diff changeset
568 file_tmp_dir = self.host.get_local_path(
7af29260ecb8 core: fix and renamed getLocalPath -> get_local_path:
Goffi <goffi@goffi.org>
parents: 3859
diff changeset
569 None, C.FILES_TMP_DIR, peer_jid.userhost(), component=True
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
570 )
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
571 file_tmp_path = file_data["file_path"] = files_utils.get_unique_name(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
572 file_tmp_dir / filename
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
573 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
574
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
575 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
576 lambda __: defer.ensureDeferred(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
577 self.register_received_file(client, peer_jid, file_data, file_tmp_path)
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
578 )
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
579 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
580
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
581 self._f.open_file_write(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
582 client, file_tmp_path, transfer_data, file_data, stream_object
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
583 )
3701
6d298323eed2 plugin comp file sharing: don't use Deferred in return value
Goffi <goffi@goffi.org>
parents: 3677
diff changeset
584 return False, True
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
585
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
586 async def _retrieve_files(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
587 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
588 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
589 """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
590 peer_jid = session["peer_jid"]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
591 if session["local_jid"].user:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
592 owner = client.get_owner_from_jid(session["local_jid"])
3514
6d9c9e2dca0a component (file sharing): fix retrieving owner from jid when user part is not specified
Goffi <goffi@goffi.org>
parents: 3507
diff changeset
593 else:
6d9c9e2dca0a component (file sharing): fix retrieving owner from jid when user part is not specified
Goffi <goffi@goffi.org>
parents: 3507
diff changeset
594 owner = peer_jid
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
595 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
596 found_files = await self.host.memory.get_files(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
597 client,
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
598 peer_jid=peer_jid,
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
599 name=file_data.get("name"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
600 file_hash=file_data.get("file_hash"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
601 hash_algo=file_data.get("hash_algo"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
602 path=file_data.get("path"),
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
603 namespace=file_data.get("namespace"),
3367
90aee1f2d07c component file sharing: added missing `owner` filtering in `_retrieveFiles`
Goffi <goffi@goffi.org>
parents: 3363
diff changeset
604 owner=owner,
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
605 )
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
606 except exceptions.NotFound:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
607 found_files = None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
608 except exceptions.PermissionError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
609 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
610 _("{peer_jid} is trying to access an unauthorized file: {name}").format(
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
611 peer_jid=peer_jid, name=file_data.get("name")
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
612 )
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
613 )
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
614 return False
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
615
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
616 if not found_files:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
617 log.warning(
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
618 _("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
619 )
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
620 return False
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
621
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
622 # we only use the first found file
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
623 found_file = found_files[0]
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
624 if found_file["type"] != C.FILE_TYPE_FILE:
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
625 raise TypeError(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
626 "a file was expected, type is {type_}".format(type_=found_file["type"])
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
627 )
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
628 file_hash = found_file["file_hash"]
3363
e3bdfecaf1b0 component file sharing: fix used of `self.files_path` in `_retrieveFiles`
Goffi <goffi@goffi.org>
parents: 3359
diff changeset
629 file_path = self.files_path / file_hash
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
630 file_data["hash_hasher"] = hasher = self._h.get_hasher(found_file["hash_algo"])
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
631 size = file_data["size"] = found_file["size"]
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
632 file_data["file_hash"] = file_hash
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
633 file_data["hash_algo"] = found_file["hash_algo"]
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
634
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
635 # 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
636 if "name" not in file_data:
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
637 file_elt.addElement("name", content=found_file["name"])
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
638 file_elt.addElement("size", content=str(size))
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
639 content_data["stream_object"] = stream.FileStreamObject(
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
640 self.host,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
641 client,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
642 file_path,
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
643 uid=self._jf.get_progress_id(session, content_name),
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
644 size=size,
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
645 data_cb=lambda data: hasher.update(data),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
646 )
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
647 return True
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
648
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
649 def _file_sending_request_trigger(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
650 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
651 ):
2504
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
652 if not client.is_component:
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
653 return True, None
67cc54b01a12 plugin file sharing component: first draft:
Goffi <goffi@goffi.org>
parents:
diff changeset
654 else:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
655 return (
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
656 False,
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
657 defer.ensureDeferred(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
658 self._retrieve_files(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
659 client, session, content_data, content_name, file_data, file_elt
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
660 )
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
661 ),
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
662 )
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
663
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
664 ## HTTP Upload ##
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
665
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
666 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
667 try:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
668 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
669 except KeyError:
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
670 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
671
4021
412b99c29d83 core (xmpp), component file sharing, plugin XEP-0363: `enabled_features` + HTTP Upload:
Goffi <goffi@goffi.org>
parents: 3932
diff changeset
672 async def _on_http_upload(self, client, request):
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
673 # filename should be already cleaned, but it's better to double check
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
674 assert "/" not in request.filename
3359
000b6722bd35 plugin XEP-0329: added `FISCreateDir` method:
Goffi <goffi@goffi.org>
parents: 3348
diff changeset
675 # client._file_sharing_allowed_hosts is set in plugin XEP-0329
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
676 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
677 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
678
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
679 quota = self.get_quota(client, request.from_)
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
680 if quota is not None:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
681 used_space = await self.host.memory.file_get_used_space(client, request.from_)
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
682
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
683 if (used_space + request.size) > quota:
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
684 raise error.StanzaError(
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
685 "not-acceptable",
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
686 text=OVER_QUOTA_TXT.format(
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
687 quota=utils.get_human_size(quota),
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
688 used_space=utils.get_human_size(used_space),
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
689 file_size=utils.get_human_size(request.size),
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
690 ),
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
691 appCondition=self._hu.get_file_too_large_elt(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
692 max(quota - used_space, 0)
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
693 ),
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
694 )
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
695
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
696 upload_id = shortuuid.ShortUUID().random(length=30)
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
697 assert "/" not in upload_id
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
698 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
699 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
700 url = urljoin(client._file_sharing_base_url, f"{upload_id}/{request.filename}")
3528
849374e59178 component file sharing: quotas implementation:
Goffi <goffi@goffi.org>
parents: 3514
diff changeset
701 slot = self._hu.Slot(
3289
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
702 put=url,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
703 get=url,
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
704 headers=[],
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
705 )
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
706 return slot
9057713ab124 plugin comp file sharing: files can now be uploaded/downloaded via HTTP:
Goffi <goffi@goffi.org>
parents: 3136
diff changeset
707
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
708 ## metadata triggers ##
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
709
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
710 def _add_file_metadata_elts(self, client, file_elt, extra_args):
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
711 # affiliation
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
712 affiliation = extra_args.get("affiliation")
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
713 if affiliation is not None:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
714 file_elt.addElement((NS_FS_AFFILIATION, "affiliation"), content=affiliation)
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
715
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
716 # comments
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
717 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
718 comments_url = extra_args.pop("comments_url")
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
719 except KeyError:
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
720 return
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
721
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
722 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
723
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
724 try:
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
725 count = len(extra_args["extra"]["comments"])
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
726 except KeyError:
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
727 count = 0
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
728
3028
ab2696e34d29 Python 3 port:
Goffi <goffi@goffi.org>
parents: 2929
diff changeset
729 comment_elt["count"] = str(count)
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
730 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
731
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
732 def _get_file_metadata_elts(self, client, file_elt, file_data):
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
733 # affiliation
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
734 try:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
735 affiliation_elt = next(file_elt.elements(NS_FS_AFFILIATION, "affiliation"))
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
736 except StopIteration:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
737 pass
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
738 else:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
739 file_data["affiliation"] = str(affiliation_elt)
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
740
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
741 # comments
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
742 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
743 comments_elt = next(file_elt.elements(NS_COMMENTS, "comments"))
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
744 except StopIteration:
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
745 pass
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
746 else:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
747 file_data["comments_url"] = str(comments_elt)
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
748 file_data["comments_count"] = comments_elt["count"]
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
749 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
750
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
751 def _add_file_metadata(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
752 self, client, iq_elt, iq_result_elt, owner, node_path, files_data
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
753 ):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
754 for file_data in files_data:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
755 file_data["comments_url"] = uri.build_xmpp_uri(
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
756 "pubsub",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
757 path=client.jid.full(),
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
758 node=COMMENT_NODE_PREFIX + file_data["id"],
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
759 )
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
760 return True
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
761
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
762 def _add_directory_metadata_elts(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
763 self, client, file_data, directory_elt, owner, node_path
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
764 ):
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
765 affiliation = file_data.get("affiliation")
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
766 if affiliation is not None:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
767 directory_elt.addElement(
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
768 (NS_FS_AFFILIATION, "affiliation"), content=affiliation
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
769 )
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
770
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
771 def _get_directory_metadata_elts(self, client, elt, file_data):
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
772 try:
3859
3ef988734869 core: fix calls to `domish.Element.elements`:
Goffi <goffi@goffi.org>
parents: 3778
diff changeset
773 affiliation_elt = next(elt.elements(NS_FS_AFFILIATION, "affiliation"))
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
774 except StopIteration:
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
775 pass
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
776 else:
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
777 file_data["affiliation"] = str(affiliation_elt)
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
778
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
779
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
780 class Comments_handler(pubsub.PubSubService):
2528
65e278997715 component file sharing: comments metadata:
Goffi <goffi@goffi.org>
parents: 2527
diff changeset
781 """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
782
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
783 def __init__(self, plugin_parent):
3314
5887fb414758 component file sharing: add/parse affiliation when possible
Goffi <goffi@goffi.org>
parents: 3301
diff changeset
784 super(Comments_handler, self).__init__()
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
785 self.host = plugin_parent.host
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
786 self.plugin_parent = plugin_parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
787 self.discoIdentity = {
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
788 "category": "pubsub",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
789 "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
790 "name": "files commenting service",
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
791 }
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
792
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
793 def _get_file_id(self, nodeIdentifier):
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
794 if not nodeIdentifier.startswith(COMMENT_NODE_PREFIX):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
795 raise error.StanzaError("item-not-found")
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
796 file_id = nodeIdentifier[len(COMMENT_NODE_PREFIX) :]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
797 if not file_id:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
798 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
799 return file_id
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
800
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
801 async def get_file_data(self, requestor, nodeIdentifier):
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
802 file_id = self._get_file_id(nodeIdentifier)
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
803 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
804 files = await self.host.memory.get_files(self.parent, requestor, file_id)
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
805 except (exceptions.NotFound, exceptions.PermissionError):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
806 # we don't differenciate between NotFound and PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
807 # to avoid leaking information on existing files
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
808 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
809 if not files:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
810 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
811 if len(files) > 1:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
812 raise error.InternalError("there should be only one file")
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
813 return files[0]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
814
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
815 def comments_update(self, extra, new_comments, peer_jid):
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
816 """update comments (replace or insert new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
817
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
818 @param extra(dict): extra data to update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
819 @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
820 @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
821 """
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
822 current_comments = extra.setdefault("comments", [])
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
823 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
824 updated = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
825 # 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
826 # exist, in which case we must update
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
827 for idx, comment in enumerate(current_comments):
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
828 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
829 if comment_id in new_comments_by_id:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
830 # 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
831 if peer_jid and comment[1] != peer_jid:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
832 # 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
833 raise exceptions.PermissionError
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
834 # we replace old_comment with updated one
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
835 new_comment = new_comments_by_id[comment_id]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
836 current_comments[idx] = new_comment
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
837 updated.append(new_comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
838
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
839 # 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
840 # the ones to insert
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
841 for comment in updated:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
842 new_comments.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
843
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
844 current_comments.extend(new_comments)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
845
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
846 def comments_delete(self, extra, comments):
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
847 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
848 comments_dict = extra["comments"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
849 except KeyError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
850 return
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
851 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
852 try:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
853 comments_dict.remove(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
854 except ValueError:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
855 continue
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
856
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
857 def _get_from(self, item_elt):
3721
6878745a47e8 component file sharing: fix incorrect use of `returnValue` in `items` async method
Goffi <goffi@goffi.org>
parents: 3709
diff changeset
858 """retrieve publisher of an item
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
859
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
860 @param item_elt(domish.element): <item> element
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
861 @return (unicode): full jid as string
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
862 """
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
863 iq_elt = item_elt
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
864 while iq_elt.parent != None:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
865 iq_elt = iq_elt.parent
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
866 return iq_elt["from"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
867
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
868 @ensure_deferred
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
869 async def publish(self, requestor, service, nodeIdentifier, items):
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
870 #  we retrieve file a first time to check authorisations
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
871 file_data = await self.get_file_data(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
872 file_id = file_data["id"]
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
873 comments = [(item["id"], self._get_from(item), item.toXml()) for item in items]
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
874 if requestor.userhostJID() == file_data["owner"]:
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
875 peer_jid = None
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
876 else:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
877 peer_jid = requestor.userhost()
4270
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
878 update_cb = partial(
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
879 self.comments_update, new_comments=comments, peer_jid=peer_jid
0d7bb4df2343 Reformatted code base using black.
Goffi <goffi@goffi.org>
parents: 4183
diff changeset
880 )
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
881 try:
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
882 await self.host.memory.file_update(file_id, "extra", update_cb)
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
883 except exceptions.PermissionError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
884 raise error.StanzaError("not-authorized")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
885
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
886 @ensure_deferred
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
887 async def items(self, requestor, service, nodeIdentifier, maxItems, itemIdentifiers):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
888 file_data = await self.get_file_data(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
889 comments = file_data["extra"].get("comments", [])
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
890 if itemIdentifiers:
3721
6878745a47e8 component file sharing: fix incorrect use of `returnValue` in `items` async method
Goffi <goffi@goffi.org>
parents: 3709
diff changeset
891 return [generic.parseXml(c[2]) for c in comments if c[0] in itemIdentifiers]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
892 else:
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
893 return [generic.parseXml(c[2]) for c in comments]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
894
3541
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
895 @ensure_deferred
888109774673 core: various changes and fixes to work with new storage and D-Bus bridge:
Goffi <goffi@goffi.org>
parents: 3532
diff changeset
896 async def retract(self, requestor, service, nodeIdentifier, itemIdentifiers):
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
897 file_data = await self.get_file_data(requestor, nodeIdentifier)
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
898 file_id = file_data["id"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
899 try:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
900 comments = file_data["extra"]["comments"]
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
901 except KeyError:
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
902 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
903
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
904 to_remove = []
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
905 for comment in comments:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
906 comment_id = comment[0]
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
907 if comment_id in itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
908 to_remove.append(comment)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
909 itemIdentifiers.remove(comment_id)
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
910 if not itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
911 break
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
912
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
913 if itemIdentifiers:
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
914 # 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
915 raise error.StanzaError("item-not-found")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
916
2624
56f94936df1e code style reformatting using black
Goffi <goffi@goffi.org>
parents: 2562
diff changeset
917 if requestor.userhostJID() != file_data["owner"]:
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
918 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
919 raise error.StanzaError("not-authorized")
2527
a201194fc461 component file sharing: comments handling first draft:
Goffi <goffi@goffi.org>
parents: 2522
diff changeset
920
4037
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
921 remove_cb = partial(self.comments_delete, comments=to_remove)
524856bd7b19 massive refactoring to switch from camelCase to snake_case:
Goffi <goffi@goffi.org>
parents: 4021
diff changeset
922 await self.host.memory.file_update(file_id, "extra", remove_cb)