Skip to content

Instantly share code, notes, and snippets.

@bleonard252
Last active August 6, 2023 15:47
Show Gist options
  • Save bleonard252/8c3981864f3be85f60b9ae2590ca0e6b to your computer and use it in GitHub Desktop.
Save bleonard252/8c3981864f3be85f60b9ae2590ca0e6b to your computer and use it in GitHub Desktop.
Python file for Titan uploads on GmCapsule
#!/usr/bin/env python3
# upload.py - Titan script for remote management
# Copyright (c) 2023 Blake Leonard
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
# == CONFIG ==
SITES_BASE_PATH = "/var/gemini/" # if you have "static" pointed somewhere else, set this too
ACCEPTED_FINGERPRINTS = ['InsertTokenFingerprintHere']
# ============
# == USAGE ==
# Put the following section in your GmCapsule config file,
# ADJUST IT:
#
# [cgi.titan]
# path = /*
# # host = gemini.example
# protocol = titan
# command = python3 /path/to/upload.py
#
# Use one of the following "tokens" to do other actions:
# <delete|remove|rm|del|x>: One of these words as the token will delete the file. Returns code 52.
# (Unfinished, commented out) <moveto|move|mv|rename> /<ABSOLUTE_FILE_PATH>: Moves the current file to the specified path. Redirects to Titan on the new path on success.
# (Planned) <copy|cp> /<ABSOLUTE_FILE_PATH>: Copies the current file to the specified path. Redirects to Titan on the same path on success.
# (Planned) import /<ABSOLUTE_FILE_PATH>: Sets the contents of the current file to the contents of the file at the specified path. Redirects to Gemini on the same path on success.
# No token: Uploads to the current file.
if os.environ.get('SERVER_PROTOCOL') != "TITAN":
sys.stdout.write("59 Wrong protocol (expected TITAN, got %s)\r\n" % os.environ.get('SERVER_PROTOCOL'))
sys.exit(0)
if os.environ.get('REMOTE_IDENT') is None:
sys.stdout.write("30 %s\r\n" % os.environ.get('GEMINI_URL'))
sys.exit(0)
elif os.environ.get('REMOTE_IDENT')[:64] not in ACCEPTED_FINGERPRINTS:
sys.stdout.write("61 You do not have the power to edit\r\n")
sys.exit(0)
def get_path(**kwargs):
return os.path.join(SITES_BASE_PATH, os.environ.get('SERVER_NAME'), (kwargs.get('path', os.environ.get('PATH_INFO')))[1:])
delete_words = ['delete', 'remove', 'del', 'rm', 'x']
move_words = ['moveto', 'move', 'mv', 'rename']
if os.environ.get('TITAN_TOKEN') in delete_words:
os.remove(get_path())
sys.stdout.write("52 Deleted successfully\r\n")
sys.exit(0)
#elif os.environ.get('TITAN_TOKEN') and str(os.environ.get('TITAN_TOKEN')).split()[0] in move_words:
# os.rename(get_path(), get_path(path=os.environ.get('TITAN_TOKEN').split(' ')[1]))
# sys.stdout.write("30 titan://{os.environ.get('SERVER_NAME')}/{str(os.environ.get('TITAN_TOKEN')).split()[1]}\r\n")
# sys.exit(0)
else:
path = get_path()
if os.path.isdir(get_path()):
path = os.path.join(get_path(), "index.gmi")
if not os.path.exists(get_path()):
os.makedirs(get_path(), exist_ok=True)
elif not os.path.exists(os.path.dirname(get_path())):
os.makedirs(os.path.dirname(get_path()))
file = open(path, mode='wb')
file.write(sys.stdin.buffer.read())
file.close()
sys.stdout.write(f"30 gemini://{os.environ.get('SERVER_NAME')}/{os.environ.get('PATH_INFO')}\r\n")
sys.exit(0)
sys.stdout.write("42 Not implemented\r\n")
sys.exit(0)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment