This repository has been archived by the owner on Apr 26, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Add an admin api to delete local media. #8519
Merged
anoadragon453
merged 8 commits into
matrix-org:develop
from
dklimpel:admin_delete_media
Oct 26, 2020
Merged
Changes from 2 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8e63746
Add an admin api to delete local media.
dklimpel 1dc57c7
add newsfile
dklimpel d8167de
Add additional api for request by date or size
dklimpel e853bc5
tox
dklimpel be9ee45
Apply suggestions from code review - Part 1
dklimpel 542a11e
Apply suggestions from code review - Part 2
dklimpel 1cb5c44
Add query and test for media that is never accessed
dklimpel 3b9a691
Apply suggestions from code review
anoadragon453 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add an admin api `DELETE /_synapse/admin/v1/media/<server_name>/<media_id>` to delete a single file from server. Contributed by @dklimpel. | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright 2020 Dirk Klimpel | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import os | ||
from binascii import unhexlify | ||
|
||
import synapse.rest.admin | ||
from synapse.api.errors import Codes | ||
from synapse.rest.client.v1 import login | ||
from synapse.rest.media.v1.filepath import MediaFilePaths | ||
|
||
from tests import unittest | ||
|
||
|
||
class DeleteMediaByIDTestCase(unittest.HomeserverTestCase): | ||
|
||
servlets = [ | ||
synapse.rest.admin.register_servlets, | ||
synapse.rest.admin.register_servlets_for_media_repo, | ||
login.register_servlets, | ||
] | ||
|
||
def prepare(self, reactor, clock, hs): | ||
self.handler = hs.get_device_handler() | ||
self.media_repo = hs.get_media_repository_resource() | ||
self.server_name = hs.hostname | ||
|
||
self.admin_user = self.register_user("admin", "pass", admin=True) | ||
self.admin_user_tok = self.login("admin", "pass") | ||
|
||
self.filepaths = MediaFilePaths(hs.config.media_store_path) | ||
|
||
def test_no_auth(self): | ||
""" | ||
Try to delete media without authentication. | ||
""" | ||
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345") | ||
|
||
request, channel = self.make_request("DELETE", url, b"{}") | ||
self.render(request) | ||
|
||
self.assertEqual(401, int(channel.result["code"]), msg=channel.result["body"]) | ||
self.assertEqual(Codes.MISSING_TOKEN, channel.json_body["errcode"]) | ||
|
||
def test_requester_is_no_admin(self): | ||
""" | ||
If the user is not a server admin, an error is returned. | ||
""" | ||
self.other_user = self.register_user("user", "pass") | ||
self.other_user_token = self.login("user", "pass") | ||
|
||
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345") | ||
|
||
request, channel = self.make_request( | ||
"DELETE", url, access_token=self.other_user_token, | ||
) | ||
self.render(request) | ||
|
||
self.assertEqual(403, int(channel.result["code"]), msg=channel.result["body"]) | ||
self.assertEqual(Codes.FORBIDDEN, channel.json_body["errcode"]) | ||
|
||
def test_media_does_not_exist(self): | ||
""" | ||
Tests that a lookup for a media that does not exist returns a 404 | ||
""" | ||
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, "12345") | ||
|
||
request, channel = self.make_request( | ||
"DELETE", url, access_token=self.admin_user_tok, | ||
) | ||
self.render(request) | ||
|
||
self.assertEqual(404, channel.code, msg=channel.json_body) | ||
self.assertEqual(Codes.NOT_FOUND, channel.json_body["errcode"]) | ||
|
||
def test_media_is_not_local(self): | ||
""" | ||
Tests that a lookup for a media that is not a local returns a 400 | ||
""" | ||
url = "/_synapse/admin/v1/media/%s/%s" % ("unknown_domain", "12345") | ||
|
||
request, channel = self.make_request( | ||
"DELETE", url, access_token=self.admin_user_tok, | ||
) | ||
self.render(request) | ||
|
||
self.assertEqual(400, channel.code, msg=channel.json_body) | ||
self.assertEqual("Can only delete local media", channel.json_body["error"]) | ||
|
||
def test_delete_media(self): | ||
""" | ||
Tests that delete a media is successfully | ||
""" | ||
|
||
download_resource = self.media_repo.children[b"download"] | ||
upload_resource = self.media_repo.children[b"upload"] | ||
image_data = unhexlify( | ||
b"89504e470d0a1a0a0000000d4948445200000001000000010806" | ||
b"0000001f15c4890000000a49444154789c63000100000500010d" | ||
b"0a2db40000000049454e44ae426082" | ||
) | ||
|
||
# Upload some media into the room | ||
response = self.helper.upload_media( | ||
upload_resource, image_data, tok=self.admin_user_tok, expect_code=200 | ||
) | ||
# Extract media ID from the response | ||
server_and_media_id = response["content_uri"][6:] # Cut off 'mxc://' | ||
server_name, media_id = server_and_media_id.split("/") | ||
|
||
self.assertEqual(server_name, self.server_name) | ||
|
||
# Attempt to access media | ||
request, channel = self.make_request( | ||
"GET", | ||
server_and_media_id, | ||
shorthand=False, | ||
access_token=self.admin_user_tok, | ||
) | ||
request.render(download_resource) | ||
self.pump(1.0) | ||
|
||
# Should be successful | ||
self.assertEqual( | ||
200, | ||
channel.code, | ||
msg=( | ||
"Expected to receive a 200 on accessing media: %s" % server_and_media_id | ||
), | ||
) | ||
|
||
# Test if the file exists | ||
local_path = self.filepaths.local_media_filepath(media_id) | ||
self.assertTrue(os.path.exists(local_path)) | ||
|
||
url = "/_synapse/admin/v1/media/%s/%s" % (self.server_name, media_id) | ||
|
||
# Delete media | ||
request, channel = self.make_request( | ||
"DELETE", url, access_token=self.admin_user_tok, | ||
) | ||
self.render(request) | ||
|
||
self.assertEqual(200, channel.code, msg=channel.json_body) | ||
self.assertEqual(1, channel.json_body["deleted"]) | ||
|
||
# Attempt to access media | ||
request, channel = self.make_request( | ||
"GET", | ||
server_and_media_id, | ||
shorthand=False, | ||
access_token=self.admin_user_tok, | ||
) | ||
request.render(download_resource) | ||
self.pump(1.0) | ||
self.assertEqual( | ||
404, | ||
channel.code, | ||
msg=( | ||
"Expected to receive a 404 on accessing deleted media: %s" | ||
% server_and_media_id | ||
), | ||
) | ||
|
||
# Test if the file is deleted | ||
self.assertFalse(os.path.exists(local_path)) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs a small update.