Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.PHONY: all
.IGNORE: lint format
.PHONY: lint format test clean
.IGNORE: format

lint:
ruff format --check .
Expand Down
15 changes: 6 additions & 9 deletions ctfcli/cli/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,18 @@ def add(self, path):
api = API()

filename = os.path.basename(path)
new_file = (filename, open(path, mode="rb"))
location = f"media/{filename}"
file_payload = {
"type": "page",
"location": location,
}

# Specifically use data= here to send multipart/form-data
r = api.post("/api/v1/files", files={"file": new_file}, data=file_payload)
r.raise_for_status()
resp = r.json()
server_location = resp["data"][0]["location"]

# Close the file handle
new_file[1].close()
with open(path, mode="rb") as file_handle:
# Specifically use data= here to send multipart/form-data
r = api.post("/api/v1/files", files={"file": (filename, file_handle)}, data=file_payload)
r.raise_for_status()
resp = r.json()
server_location = resp["data"][0]["location"]

config.config.set("media", location, f"/files/{server_location}")

Expand Down
12 changes: 6 additions & 6 deletions ctfcli/core/api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Mapping
from collections.abc import Mapping
from urllib.parse import urljoin

from requests import Session
Expand Down Expand Up @@ -76,28 +76,28 @@ def request(self, method, url, data=None, files=None, *args, **kwargs):
headers = dict(headers)
headers["Content-Type"] = multipart.content_type

return super(API, self).request(
return super().request(
method,
url,
*args,
data=multipart,
headers=headers,
*args,
**kwargs,
)

# otherwise set the content-type to application/json for all API requests
# modify the headers here instead of using self.headers because we don't want to
# override the multipart/form-data case above
if data is None and files is None:
if kwargs.get("headers", None) is None:
if kwargs.get("headers") is None:
kwargs["headers"] = {}
kwargs["headers"]["Content-Type"] = "application/json"

return super(API, self).request(
return super().request(
method,
url,
*args,
data=data,
files=files,
*args,
**kwargs,
)
Loading