Skip to content

Commit

Permalink
add preview and controllable bulk download
Browse files Browse the repository at this point in the history
  • Loading branch information
PaddeCraft committed Apr 21, 2022
1 parent cf63dff commit 3774071
Show file tree
Hide file tree
Showing 14 changed files with 430 additions and 41 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
graft upstream/templates
graft upstream/static
global-exclude __pycache__
global-exclude *.py[cod]
5 changes: 1 addition & 4 deletions upstream/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1 @@
from pytz import VERSION


VERSION = "0.1.0"
VERSION = "0.1.1"
95 changes: 63 additions & 32 deletions upstream/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
stream_with_context,
)
from werkzeug.utils import secure_filename
from upstream.sourceCodeFormats import formats as sourceCodeFormats
from upstream.__init__ import VERSION
from upstream.getFileIcon import getFileIcon
from datetime import datetime
Expand Down Expand Up @@ -105,33 +106,26 @@ def getDirSize(pth):
return dirTreeSize


def scanDir(dir: str, depth: int = 0, oldFileList: list = []) -> list:
fileList = oldFileList
indexed = []
found = 0
for i in glob.glob(os.path.join(dir, "*" + ("/*" * depth))):
found += 1
indexed.append(i.lower().replace("\\", "/"))
if found == 0:
return fileList
else:
fileList += indexed
scanDir(dir, depth + 1, fileList)
def scanDir(dir) -> list:
files = []
for root, _, files in os.walk(dir):
for file in files:
yield os.path.join(root, file)


def zipFolder(path, processUUID):

def zipFolder(path, files, processUUID):
global zippingProcesses
zippingProcesses.append({"uuid": processUUID, "path": path})
with zipfile.ZipFile(
os.path.join(basePath, "upstream-tmp", f"{processUUID}.zip"),
"w",
) as zipf:
for root, _, files in os.walk(path):
for file in files:
zipf.write(
os.path.join(root, file),
os.path.join(root, file).replace("\\", "/").replace(path, ""),
)
for file in files:
zipf.write(
file,
file.replace("\\", "/").replace(path, ""),
)
# https://stackoverflow.com/a/43049879
zippingProcesses.pop(
list(map(itemgetter("uuid"), zippingProcesses)).index(processUUID)
Expand Down Expand Up @@ -187,35 +181,74 @@ def explore(path):
currentPath=path.replace("\\", "/"),
)
else:
return "Not implemented yet"
pth = getPath(path)
ext = pathlib.Path(pth).suffix.lower()[1:]
dwnLoadPath = "/download/" + path
if ext == "pdf":
return render_template(
"preview/pdf.html",
file=dwnLoadPath,
breadcrumbs=breadcrumbGenerator(pth),
)
elif ext in ["jpg", "jpeg", "png", "gif"]:
return render_template(
"preview/image.html",
file=dwnLoadPath,
breadcrumbs=breadcrumbGenerator(pth),
)
elif ext in sourceCodeFormats:
return render_template(
"preview/code.html",
file=dwnLoadPath,
breadcrumbs=breadcrumbGenerator(pth),
)
else:
return redirect(dwnLoadPath)



@app.route("/download/<path:path>")
def download(path):
global zippingProcesses
pth = getPath(path)
log.info(f"New download from path {pth}")
if os.path.isfile(pth):
# return send_file(pth, as_attachment=True)
log.info(f"Sending single file")
return Response(
stream_with_context(read_file_chunks(pth)),
headers={
"Content-Disposition": f'attachment; filename={pth.split("/")[-1]}',
},
)
else:
filesToZip = []
requestedFileStr = request.args.get("files")
if requestedFileStr == "*":
filesToZip = [x for x in scanDir(pth)]
else:
elements = requestedFileStr.split("||")
for element in elements:
if os.path.isfile(os.path.join(pth, element)):
filesToZip.append(os.path.join(pth, element))
else:
for file in scanDir(os.path.join(pth, element)):
filesToZip.append(file)
zipProcessUUID = str(uuid())
threading.Thread(args=(pth, zipProcessUUID), target=zipFolder).start()
threading.Thread(args=(pth, filesToZip, zipProcessUUID), target=zipFolder).start()
log.info(f"Zipping {len(filesToZip)} files")
log.info(f"Zipping process UUID: {zipProcessUUID}")
print("")
time.sleep(1)
return render_template("waitForZip.html", processUUID=zipProcessUUID)
return render_template("waitForZip.html", processUUID=zipProcessUUID, path=pth)


@app.route("/upload/<path:path>", methods=["POST"])
def upload(path):
if 'file' not in request.files:
return abort(400)
file = request.files['file']
if file.filename == '':
return redirect(f"/explore/{path}")
file = request.files['file']
filename = secure_filename(file.filename)
fp = os.path.join(getPath(path), filename)
while os.path.exists(fp):
Expand All @@ -224,30 +257,28 @@ def upload(path):
return redirect(f"/explore/{path}")


@app.route("/view/<path:path>")
def view(path):
pass


@app.route("/api/isProcessing/<_uuid>")
def isProcessing(_uuid):
global zippingProcesses
isStillProcessing = True
maxSize = 0
size = 0
try:
# https://stackoverflow.com/a/43049879
dictForUUID = zippingProcesses[
list(map(itemgetter("uuid"), zippingProcesses)).index(_uuid)
]
maxSize = getDirSize(dictForUUID["path"])
size = os.path.getsize(
os.path.join(basePath, "upstream-tmp", f"{_uuid}.zip")
),
except ValueError:
isStillProcessing = False

return json.dumps(
{
"isProcessing": isStillProcessing,
"currentSize": os.path.getsize(
os.path.join(basePath, "upstream-tmp", f"{_uuid}.zip")
),
"currentSize": size,
"maxSize": maxSize,
}
)
Expand Down
4 changes: 3 additions & 1 deletion upstream/getFileIcon.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from upstream.sourceCodeFormats import formats as sourceCodeFormats

def getFileIcon(ext):
if ext in ["jpg", "jpeg", "png", "gif", "bmp"]:
return "image"
Expand All @@ -13,7 +15,7 @@ def getFileIcon(ext):
return "archive"
elif ext in ["iso", "img", "bin", "cue", "toast", "vcd", "cdr", "dvd", "bin", "bak", "bup"]:
return "storage"
elif ext in ["html", "css", "js", "py", "cpp", "c", "csharp", "java", "cs", "json", "xml", "yml", "yaml"]:
elif ext in sourceCodeFormats: # ["html", "css", "js", "py", "cpp", "c", "csharp", "java", "cs", "json", "xml", "yml", "yaml"]:
return "code"
else:
return "insert_drive_file"
5 changes: 5 additions & 0 deletions upstream/sourceCodeFormats.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
formats = ["svg", "cpp", "py", "csharp", "css", "html", "js",
"json", "rb", "java", "php", "sql", "xml", "yml", "yaml",
"c", "go", "sh", "bat", "md", "txt", "log", "ini", "conf",
"cfg", "pl", "ino", "rs", "ts", "kt", "asm", "s", "h",
"hpp", "ps1", "vbs", "lua"]
6 changes: 6 additions & 0 deletions upstream/static/prism-dark.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions upstream/static/prism-light.css

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3774071

Please sign in to comment.