Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import cgi
- import cgitb; cgitb.enable(display=0, logdir="../logs")
- import os
- import filemanager as fm
- import shutil
- from pathlib import Path
- ### Pre-page setup ###
- LINUX_ROOT = "/home/pi/www/test/"
- APACHE_ROOT = "/pi/test/"
- blackdirs = fm.loadblacklist()
- form = cgi.FieldStorage()
- dir_selected = str(form.getvalue("directory", "None"))
- dir_zombie = dir_selected # lingering copy for error reporting
- trashitem = str(form.getvalue("trashitem", "DEFAULT"))
- newdir = str(form.getvalue("newdir", "None"))
- try:
- upload_file = form["uploadname"]
- except KeyError:
- upload_file = None
- up_check = False
- if dir_selected in blackdirs or not fm.input_check(dir_selected):
- dir_selected = None
- dir_zombie = "[ILLEGAL]" + dir_zombie
- elif not os.path.isdir(LINUX_ROOT + dir_selected):
- dir_selected = None
- dir_zombie = "[NONEXISTANT]" + dir_zombie
- # File upload code
- try:
- if upload_file.filename:
- # prevent traversal attacks and allow windows users to upload files
- fn = os.path.basename(upload_file.filename.replace("\\", "/"))
- open(LINUX_ROOT + dir_selected + "/" + fn, 'wb').write(upload_file.file.read())
- up_check = "File '" + fn + "' was uploaded successfully."
- else:
- up_check = "No file uploaded: " + str(upload_file.file)
- except AttributeError:
- # do nothing if upload_file does not exist (always occurs when page first loads)
- pass
- # File deletion code
- if trashitem != "DEFAULT":
- try:
- trashname = Path(LINUX_ROOT + trashitem).name
- shutil.move(LINUX_ROOT + trashitem, LINUX_ROOT + "trash/" + trashname)
- up_check = "Item '{}' was successfully deleted.".format(trashname)
- except FileNotFoundError:
- up_check = "Item '{}' not found.".format(trashitem)
- # Directory creation code
- if newdir != "None":
- try:
- newpath = Path(LINUX_ROOT + dir_selected) / newdir
- newpath.mkdir(mode=0o775)
- up_check = "Directory successfully created."
- except FileExistsError:
- up_check = "Directory {} already exists.".format(newdir)
- except FileNotFoundError:
- up_check = "Cannot create new directory with implicit parent directories."
- ### Page construction ###
- fm.httpheader()
- if dir_selected == None:
- fm.wrongpage(dir_zombie)
- else:
- fm.dirlist(APACHE_ROOT, LINUX_ROOT, dir_selected, up_check)
- fm.httpfooter()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement