Coders Packet

Google Drive type File Explorer Web application using python3 Flask and Jquery

By Narender Bhadu

This packet is web file explorer with google drive like User Interface. With built in video player and image viewer and a terminal to execute command on a remote host.

Remote file manager web app In Python3

Features

1. file explorer

2. Terminal

3. Image and video viewer

           

library and frameworks used

1. flask

2. Jquery

3. Jhinjha 2

4. subprocess

Functions and usage

server can be started by simply running server.py file with python3

from flask import Flask,send_file
import os

# this lib will reload custom functions after every change
# hence no need to restart server after every change
import importlib

# custom functions
import functions

# flask root object
app=Flask(__name__)

# handles requests for index page
@app.route('/')
def index():
  importlib.reload(functions)
  return functions.index()

# when change directory (cd) is required
@app.route('/change_page',methods=['POST'])
def change_page():
  importlib.reload(functions)
  return functions.change_page()

# to handle terminal commands
@app.route('/execute_command',methods=['POST'])
def execute_command():
  importlib.reload(functions)
  return functions.execute_command()

# to serve files other than index page
@app.route('/',methods=['GET'])
def requested_files(filename):
  importlib.reload(functions)
  # to edit any file in text editor
  if filename == "file.html":
    return functions.files()
  # favion icon
  elif filename == "favicon.ico":
    return "" # not required so just returning an empty string
  # terminal UI page
  elif filename == "terminal.html": 
    return functions.terminal_page()
  # other files from directory like png,mp4 etc
  else:
    return send_file(filename)

if __name__=="__main__":
  # uncomment only for development debbuging
  # app.config['DEBUG']=True

  # runs server @ localhost
  app.run(host="127.0.0.1",port=5000)

  #runs server at wlan network can be accesible with other devices
  # app.run(host="192.168.137.1",port=5000)

Module named functions contains helper functions

# return list of files and folder in directory
def serve_items():
  # python list of names
  title=os.getcwd()
  # we will items in group of 5 to arrange in a row
  dir_items = [[]]
  for x in os.listdir():
    # y will store absolute path of item
    y=os.path.join(title,x)
    # ext will store ext of item
    ext = x.lower().split(".",1)[-1]
    # if group is filled start new group of five items
    if len(dir_items[-1])==5:
      dir_items.append([])
    # if item is directory
    if os.path.isdir(x):
      dir_items[-1].append({'a':y,'n':x,'type':'folder'})
    # if item is a image
    elif ext in ["png","jpg"]:
      dir_items[-1].append({'a':y,'n':x,'type':'graphic',"ext":ext})
    # if item is a video
    elif ext in ["mp4","mkv","3gp"]:
      dir_items[-1].append({'a':y,'n':x,'type':'video',"ext":ext})
    # other files
    else:
      dir_items[-1].append({'a':y,'n':x,'type':'file'})

  return dir_items

def index():
  # return app.html page
  return render_template("explorer.html",title="file explorer",dir_items=serve_items())

def terminal_page():
  # return terminal UI page
  return render_template("terminal.html")

def change_page():
  # returns content of changed directory after cd
  cd = request.values.get("cd")
  os.chdir(cd)
  return render_template("serve_items.html",dir_items=serve_items())

def files():
  # return file editor page
  return render_template("file.html")

def execute_command():
  # return result of command
  cmd = request.values.get("cmd")
  return getoutput(cmd).replace("\n","
")