Advertisement
Guest User

runner.oy refactored

a guest
Sep 15th, 2019
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.37 KB | None | 0 0
  1. !/usr/bin/sage
  2. from flask import Flask, render_template, flash, request, redirect, url_for, send_from_directory
  3. from werkzeug.utils import secure_filename
  4. from sagesham import SS, rederive
  5. from secrets import REQUIRED, FLAG, APP_SECRET
  6. from music import *
  7. import uuid
  8. import os
  9. import random
  10. import pickle
  11.  
  12. app = Flask(__name__)
  13. app.config["RAW_FILES"] = "./tmp/"
  14. shares = []
  15.  
  16. def config():
  17. # total_pieces = 4000
  18. # required_pieces = REQUIRED
  19. # prime = 101109149181191199401409419449461491499601619641661691809811881911
  20. # secret = int(FLAG.encode('hex'),16)
  21. # sham = SS(secret, total_pieces, required_pieces, prime)
  22. # return sham.create_shares()
  23. return pickle.load(open('pshares','rb'))
  24.  
  25. @app.route('/')
  26. def index():
  27. return render_template('index.html')
  28.  
  29. @app.route('/musicin', methods=['POST'])
  30. def send_music():
  31. global shares
  32. if 'file' not in request.files:
  33. return redirect(url_for("index", _anchor="features"))
  34. file = request.files['file']
  35. if file.filename =='':
  36. return redirect(url_for("index", _anchor="features"))
  37.  
  38. name = os.path.join(app.config['RAW_FILES'], str(uuid.uuid4()) + secure_filename(file.filename))
  39. file.save(name)
  40. share = random.choice(shares)
  41. try:
  42. magic_name = os.path.join('musicals',str(uuid.uuid4()))
  43. create_score(name, hex(int(share[1])).replace('0x','').replace('L',''), str(int(share[0])), magic_name)
  44. return redirect('/sheetmusic/{}'.format(magic_name))
  45. except Exception as e:
  46. print e
  47. return redirect(url_for("index", _anchor="features"))
  48. return redirect(url_for("index", _anchor="features"))
  49.  
  50. @app.after_request
  51. def remove_files(request):
  52. #14% possibility to remove files
  53. if (random.randint(0,100) < 14):
  54. os.system('find musicals/ -type f -mmin +10 -delete')
  55. os.system('find tmp/ -type f -mmin +10 -delete')
  56. return request
  57.  
  58. @app.route('/musicals/<path:path>')
  59. def serve_musical(path):
  60. return send_from_directory('musicals', path)
  61.  
  62. @app.route('/sheetmusic/musicals/<filename>', methods=['GET'])
  63. def render_template_musical(filename):
  64. return render_template("musical.html", filename="/musicals/"+filename)
  65.  
  66. if __name__ == '__main__':
  67. app.secret_key = APP_SECRET
  68. environment.UserSettings()['warnings']=0
  69. shares = config()
  70. app.run(host='0.0.0.0')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement