Advertisement
ETHERNET2

filemanager.py

Dec 5th, 2019
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.84 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. import os
  3. from pathlib import Path
  4.  
  5. def httpheader():
  6.     print("Content-Type: text/html")
  7.  
  8.     print("""
  9.    <!DOCTYPE html>
  10.    <html>
  11.    <head>
  12.      <title>HTTP File Browser</title>
  13.    """)
  14.  
  15.     print('<link rel="stylesheet" href="{}">'.format("../style.css"))
  16.  
  17.     print("""
  18.    </head>
  19.    <body>
  20.    """)
  21.  
  22. def httpfooter():
  23.     print("""
  24.    <br>
  25.    <a href="../index.html">Home</a>
  26.    <br><br><br>
  27.    
  28.    <footer>
  29.        All icons used are sourced from www.iconarchive.com
  30.        under the <a href="https://creativecommons.org/licenses/by-nc-nd/4.0/">CC BY-NC-ND 4.0</a> license. <br>
  31.        Artists: Hopstart, IconsMind
  32.    </footer>
  33.    </body>
  34.    </html>
  35.    """)
  36.  
  37. def loadblacklist(dir_blacklist="blacklist"):
  38.     with open(dir_blacklist, "r") as f:
  39.         safe_dirlist = [line.rstrip() for line in f]
  40.     return safe_dirlist
  41.  
  42. def input_check(inp_str):
  43.     'Returns false if [inp_str] fails sanitation checks'
  44.     all_same = lambda s: s == len(s) * s[0]
  45.     if "." in inp_str:
  46.         return False
  47.     elif all_same(inp_str) and inp_str[0] == "/":
  48.         return False
  49.     else:
  50.         return True
  51.  
  52. def subdir(subdir_path, delmode=True):
  53.    
  54.     dir_icon = '<img src="icons/folder.png" class="icon">'
  55.    
  56.     frm_head = '<form action="filemanscript.py" method="post">'
  57.     subdir_button = "<input type='submit' class='subdirbutton' name='directory' value='{}' />".format(subdir_path)
  58.    
  59.     backdir_data = "<input type='hidden' name='directory' value='{}' />".format(str(Path(subdir_path).parent))
  60.     trash_button = "<button type='submit' class='del_button' name='trashitem' value='{}'>Delete</button>".format(subdir_path)
  61.    
  62.     trash_form = frm_head + backdir_data + trash_button + "</form>"
  63.    
  64.     if not delmode:
  65.         trash_form = ""
  66.         dir_icon = '<img src="icons/back.png" class="icon">'
  67.    
  68.     subdir_form = frm_head + dir_icon + subdir_button + "</form>"
  69.    
  70.     return "<div style='display: flex;'>" + subdir_form + trash_form + "</div>"
  71.  
  72. def dirlist(server_root, local_root, dir_selected, up_msg=False):
  73.     print("<h1>" + dir_selected + "/</h1>")
  74.     print("<div style='display: flex;'>")
  75.  
  76.     # File upload block
  77.     print("""
  78.    <form enctype="multipart/form-data" style="padding-right: 15px;" action="filemanscript.py" method="post">
  79.    <h4>File Upload</h4>
  80.    <p><input type="file" name="uploadname" /><input type="submit" value="Upload" /></p>
  81.    """)
  82.     print("<input type='hidden' name='directory' value='{}' />".format(dir_selected))
  83.     print("</form>")
  84.    
  85.     # Mkdir block
  86.     print("<form action='filemanscript.py' method='post'>")
  87.     print("<h4>Create Directory</h4>")
  88.     print("<p><input type='text' name='newdir' /> <input type='submit' value='Make' /></p>")
  89.     print("<input type='hidden' name='directory' value='{}' />".format(dir_selected))
  90.     print("</form>")
  91.    
  92.     print("</div>")
  93.  
  94.     if up_msg: print("<p>" + up_msg + "</p>")
  95.     print("<hr>")
  96.    
  97.    
  98.     dir_list = os.listdir(local_root + dir_selected)
  99.     dir_list.sort()
  100.     dir_server = server_root + dir_selected
  101.    
  102.     # link_order is used to allow directories to be put at the top of the page
  103.     link_order = []
  104.  
  105.     # File download block
  106.     for filename in dir_list:
  107.         linux_path = local_root + dir_selected + "/" + filename
  108.         server_path = dir_server + "/" + filename
  109.  
  110.         if os.path.isdir(linux_path):
  111.             link_order.insert(0, subdir(dir_selected + "/" + filename))
  112.         else:
  113.             frm_head = """
  114.            <form id="man_form" action="filemanscript.py" method="post">
  115.            <input type='hidden' name='directory' value='{}' />
  116.            """.format(dir_selected)
  117.            
  118.             dwn_button = "<a class='button' href='{}' download>Download</a>".format(server_path)
  119.  
  120.             prv_button = "<a class='button' href='{}'>Preview</a>".format(server_path)
  121.            
  122.             del_button = "<button type='submit' class='del_button' name='trashitem' value='{}'>Delete</button>".format(
  123.             dir_selected + "/" + filename)
  124.            
  125.             link_order.append("{}<p><img src='icons/file.png' class='icon'>{}{}{}{}</p></form>".format(
  126.             frm_head, filename, dwn_button, prv_button, del_button))
  127.    
  128.     if len(dir_selected.split("/")) > 1:
  129.         # Add button to go back to previous directory
  130.        
  131.         back_dir = str(Path(dir_selected).parent)
  132.         link_order.insert(0, subdir(back_dir, delmode=False))
  133.    
  134.     for item in link_order:
  135.         print(item)
  136.  
  137. def wrongpage(dir_selected):
  138.     print("<h1>Something's gone wrong</h1>")
  139.     print("You're attempting to access either an illegal or<br>")
  140.     print("nonexistant directory.<br>")
  141.     print("The culprit:<br>")
  142.     print()
  143.     print("<h3>Selected directory: " + dir_selected + "</h3>")
  144.     print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement