-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to store manage media files locally and reference their U…
…RLs in Pages (#156) * Add `ctf media add`, `ctf media rm`, `ctf media url` commands * Allows ctfcli repos to manage files locally and reference the actual server URLs of media files in Pages * Adds concept of replacing placeholders like `{{ media/ctfd.png }}` with the actual URL on the server
- Loading branch information
Showing
5 changed files
with
114 additions
and
2 deletions.
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
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,80 @@ | ||
import os | ||
|
||
import click | ||
|
||
from ctfcli.core.api import API | ||
from ctfcli.core.config import Config | ||
|
||
|
||
class MediaCommand: | ||
def add(self, path): | ||
"""Add local media file to config file and remote instance""" | ||
config = Config() | ||
if config.config.has_section("media") is False: | ||
config.config.add_section("media") | ||
|
||
api = API() | ||
|
||
new_file = ("file", open(path, mode="rb")) | ||
filename = os.path.basename(path) | ||
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=[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() | ||
|
||
config.config.set("media", location, f"/files/{server_location}") | ||
|
||
with open(config.config_path, "w+") as f: | ||
config.write(f) | ||
|
||
def rm(self, path): | ||
"""Remove local media file from remote server and local config""" | ||
config = Config() | ||
api = API() | ||
|
||
local_location = config["media"][path] | ||
|
||
remote_files = api.get("/api/v1/files?type=page").json()["data"] | ||
for remote_file in remote_files: | ||
if f"/files/{remote_file['location']}" == local_location: | ||
# Delete file from server | ||
r = api.delete(f"/api/v1/files/{remote_file['id']}") | ||
r.raise_for_status() | ||
|
||
# Update local config file | ||
del config["media"][path] | ||
with open(config.config_path, "w+") as f: | ||
config.write(f) | ||
|
||
def url(self, path): | ||
"""Get server URL for a file key""" | ||
config = Config() | ||
api = API() | ||
|
||
if config.config.has_section("media") is False: | ||
config.config.add_section("media") | ||
|
||
try: | ||
location = config["media"][path] | ||
except KeyError: | ||
click.secho(f"Could not locate local media '{path}'", fg="red") | ||
return 1 | ||
|
||
remote_files = api.get("/api/v1/files?type=page").json()["data"] | ||
for remote_file in remote_files: | ||
if f"/files/{remote_file['location']}" == location: | ||
base_url = config["config"]["url"] | ||
base_url = base_url.rstrip("/") | ||
return f"{base_url}{location}" | ||
click.secho(f"Could not locate remote media '{path}'", fg="red") | ||
return 1 |
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,15 @@ | ||
from ctfcli.core.config import Config | ||
from ctfcli.utils.tools import safe_format | ||
|
||
|
||
class Media: | ||
@staticmethod | ||
def replace_placeholders(content: str) -> str: | ||
config = Config() | ||
try: | ||
section = config["media"] | ||
except KeyError: | ||
section = [] | ||
for m in section: | ||
content = safe_format(content, items={m: config["media"][m]}) | ||
return content |
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