applications
This commit is contained in:
		
							
								
								
									
										61
									
								
								routes/application.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										61
									
								
								routes/application.py
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,61 @@
 | 
			
		||||
import datetime
 | 
			
		||||
import os.path
 | 
			
		||||
import uuid
 | 
			
		||||
 | 
			
		||||
from flask import Blueprint, request
 | 
			
		||||
 | 
			
		||||
import database
 | 
			
		||||
from constants import APKS_PATH
 | 
			
		||||
 | 
			
		||||
application_blueprint = Blueprint('application', __name__)
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@application_blueprint.before_request
 | 
			
		||||
def auth():
 | 
			
		||||
    API_KEY = 'AF9A20DD9264C134CDA0ADACED834368'
 | 
			
		||||
    if request.headers.get('Authorization') != API_KEY:
 | 
			
		||||
        return {'error': 'Unauthorized'}, 401
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@application_blueprint.get('<string:application_name>/download/<string:version>')
 | 
			
		||||
def download_version(application_name: str, version: str):
 | 
			
		||||
    application = database.Application.query.filter_by(version=version,
 | 
			
		||||
                                                       name=application_name).first()
 | 
			
		||||
    if not application:
 | 
			
		||||
        return {
 | 
			
		||||
            "error": f"Application version '{version}' not found."
 | 
			
		||||
        }, 404
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@application_blueprint.get('<string:application_name>/version')
 | 
			
		||||
def get_version(application_name: str):
 | 
			
		||||
    version = (database.Application.query.
 | 
			
		||||
               filter_by(name=application_name).
 | 
			
		||||
               order_by(database.Application.uploaded.desc()).
 | 
			
		||||
               with_entities(database.Application.version).
 | 
			
		||||
               limit(1).
 | 
			
		||||
               scalar())
 | 
			
		||||
    return {'latest_version': version}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@application_blueprint.post('<string:application_name>/upload')
 | 
			
		||||
def upload(application_name: str):
 | 
			
		||||
    file = request.files.get('file')
 | 
			
		||||
    version = request.form.get('version')
 | 
			
		||||
    if (not file) or (not version):
 | 
			
		||||
        return {"error": "Invalid form data. There is no file or version field"}, 400
 | 
			
		||||
    version = version.strip()
 | 
			
		||||
    application = (database.Application.query.
 | 
			
		||||
                   filter_by(version=version).
 | 
			
		||||
                   with_entities(database.Application.id).first())
 | 
			
		||||
    if application:
 | 
			
		||||
        return {"error": f"Specified version ({version}) already uploaded"}, 400
 | 
			
		||||
    filename = uuid.uuid4().hex + '.apk'
 | 
			
		||||
    file.save(os.path.join(APKS_PATH, filename))
 | 
			
		||||
    application = database.Application(name=application_name,
 | 
			
		||||
                                       version=version,
 | 
			
		||||
                                       uploaded=datetime.datetime.now(),
 | 
			
		||||
                                       filename=filename)
 | 
			
		||||
    database.db.session.add(application)
 | 
			
		||||
    database.db.session.commit()
 | 
			
		||||
    return {'filename': filename, 'version': version}
 | 
			
		||||
		Reference in New Issue
	
	Block a user