Initial commit
This commit is contained in:
64
web/app.py
Normal file
64
web/app.py
Normal file
@@ -0,0 +1,64 @@
|
||||
import os
|
||||
from redis import Redis
|
||||
import rq
|
||||
from rq.job import Job
|
||||
import tasks
|
||||
from flask import Flask, request, jsonify
|
||||
|
||||
app = Flask(__name__)
|
||||
queue = rq.Queue("motus", connection=Redis.from_url("redis://"))
|
||||
redis = Redis()
|
||||
|
||||
|
||||
@app.route("/")
|
||||
@app.route("/index")
|
||||
def index():
|
||||
return jsonify({"message": "OK: Hello"}), 200
|
||||
|
||||
|
||||
@app.route("/check")
|
||||
def check():
|
||||
headers = request.headers
|
||||
auth = headers.get("X-Api-Key")
|
||||
if auth != "fbdhjsbf43443refdsafa":
|
||||
return jsonify({"message": "ERROR: Unauthorized"}), 401
|
||||
|
||||
algorithm = request.args.get("type", default="sha256", type=str)
|
||||
filename = request.args.get("filename", default="", type=str)
|
||||
hashval = request.args.get("hash", default="", type=str)
|
||||
|
||||
if filename == "" or hashval == "":
|
||||
return jsonify({"message": "ERROR: Invalid Arguments"}), 400
|
||||
|
||||
if algorithm == "sha256":
|
||||
pass
|
||||
else:
|
||||
return jsonify({"message": "ERROR: Unknown Algorithm"}), 400
|
||||
|
||||
try:
|
||||
filesize = os.path.getsize(filename)
|
||||
except:
|
||||
return jsonify({"message": "ERROR: Unable to Access File"}), 400
|
||||
|
||||
job = queue.enqueue(tasks.check_hash, algorithm, filename, hashval)
|
||||
job.meta["status"] = "Processing: 0%".format(filesize)
|
||||
job.save_meta()
|
||||
|
||||
return jsonify({"message": "OK: Job Submitted", "jobid": job.get_id()}), 200
|
||||
|
||||
|
||||
@app.route("/status")
|
||||
def status():
|
||||
headers = request.headers
|
||||
auth = headers.get("X-Api-Key")
|
||||
if auth != "fbdhjsbf43443refdsafa":
|
||||
return jsonify({"message": "ERROR: Unauthorized"}), 401
|
||||
|
||||
jobid = request.args.get("jobid", type=str)
|
||||
job = Job.fetch(jobid, connection=redis)
|
||||
|
||||
try:
|
||||
meta = job.get_meta()
|
||||
return jsonify({"status": meta}), 200
|
||||
except:
|
||||
return jsonify({"status": "Failed"}), 400
|
||||
45
web/tasks.py
Normal file
45
web/tasks.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import hashlib
|
||||
import os
|
||||
|
||||
import time
|
||||
from rq import get_current_job
|
||||
|
||||
BLOCKSIZE = 65535
|
||||
|
||||
|
||||
def check_hash(algorithm, filename, hashval):
|
||||
job = get_current_job()
|
||||
if job:
|
||||
job.meta["status"] = "Processing: 0%"
|
||||
job.save_meta()
|
||||
|
||||
if filename == "" or hashval == "":
|
||||
return "ERROR: Invalid Arguents"
|
||||
|
||||
if algorithm == "sha256":
|
||||
hashfunc = hashlib.sha256()
|
||||
else:
|
||||
return "ERROR: Unknown Algorithm"
|
||||
|
||||
filesize = os.path.getsize(filename)
|
||||
filepos = 0
|
||||
with open(filename, "rb") as f:
|
||||
file_buffer = f.read(BLOCKSIZE)
|
||||
filepos += len(file_buffer)
|
||||
job.meta["status"] = "Processing: {}%".format(filepos / filesize * 100)
|
||||
job.save_meta()
|
||||
while len(file_buffer) > 0:
|
||||
hashfunc.update(file_buffer)
|
||||
file_buffer = f.read(BLOCKSIZE)
|
||||
|
||||
job.meta["status"] = "Processing: {}%".format(filesize / filesize * 100)
|
||||
job.save_meta()
|
||||
|
||||
# time.sleep(5)
|
||||
|
||||
if hashfunc.hexdigest() == hashval:
|
||||
job.meta["status"] = "OK: Hash Value Matches"
|
||||
else:
|
||||
job.meta["status"] = "FAILED: Invalid Hash"
|
||||
|
||||
job.save_meta()
|
||||
4
web/wsgi.py
Normal file
4
web/wsgi.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from app import app
|
||||
|
||||
if __name__ == "__main__":
|
||||
app.run()
|
||||
Reference in New Issue
Block a user