-
Notifications
You must be signed in to change notification settings - Fork 77
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
- Loading branch information
Showing
5 changed files
with
85 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,55 @@ | ||
import os | ||
|
||
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) |
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,11 @@ | ||
from ctfcli.core.config import Config | ||
from ctfcli.utils.tools import safe_format | ||
|
||
|
||
class Media: | ||
@staticmethod | ||
def replace_placeholders(content: str) -> str: | ||
config = Config() | ||
for m in config["media"]: | ||
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