71 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			71 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import datetime
 | 
						|
import os.path
 | 
						|
import uuid
 | 
						|
 | 
						|
from flask import Blueprint, request, send_file
 | 
						|
from flask_jwt_extended import verify_jwt_in_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 and (not verify_jwt_in_request(optional=True)):
 | 
						|
        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
 | 
						|
 | 
						|
    file_path = os.path.join(APKS_PATH, application.filename)
 | 
						|
    version_safe = version.replace('.', '_')
 | 
						|
    print(file_path)
 | 
						|
    return send_file(file_path,
 | 
						|
                     download_name=f'{application_name}_{version_safe}.apk',
 | 
						|
                     as_attachment=True,
 | 
						|
                     mimetype='application/vnd.android.package-archive')
 | 
						|
 | 
						|
 | 
						|
@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}
 |