download_manager.py 7.76 KB
Newer Older
wanli's avatar
wanli committed
1 2 3 4
#!/usr/bin/env python
# -*- coding: utf_8 -*-

import os
wanli's avatar
wanli committed
5
import re
wanli's avatar
wanli committed
6 7 8 9 10 11 12 13
import copy
import time
import types
import json
import logging
import traceback
import shutil
from collections import OrderedDict
wanli's avatar
wanli committed
14 15 16
import urllib
from urllib import parse, request
from urllib.parse import urlparse, urljoin, urlencode
wanli's avatar
wanli committed
17 18
from datetime import datetime
from pony.orm import *
wanli's avatar
wanli committed
19
from flask import request as flask_request
wanli's avatar
wanli committed
20 21 22
from app.setting import config, conf
from model import fullStackDB
from model.download import AppDownload
wanli's avatar
wanli committed
23
from model.device import Device
wanli's avatar
wanli committed
24 25
from model.apps import Apps
from model.user import User
wanli's avatar
wanli committed
26
from utils import sql_filter, ThreadMaker
wanli's avatar
wanli committed
27 28 29

logger = logging.getLogger("DownloadManager")

wanli's avatar
wanli committed
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
@ThreadMaker
def update_download_information(ip, id):
    params = { 'ak': 'aZEAgYG8wKuLd6DS9BmCloGtfnGGkRMn', 'coor': 'bd09ll' }
    parameters = urllib.parse.urlencode(params)
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Trident/7.0; rv:11.0) like Gecko'
    }

    if ip != '127.0.0.1':
        params.update({'ip': ip})
        parameters = urllib.parse.urlencode(params)

    url = 'http://api.map.baidu.com/location/ip'
    req = request.Request(url='%s%s%s' % (url, '?', parameters), headers=headers)
    ret = request.urlopen(req).read()
    jsonData = json.loads(ret)
    logger.info(jsonData)

    if (0 != jsonData['status']):
        return None

    with db_session:
        down = AppDownload.get(id=id)
        if down:
wanliofficial's avatar
wanliofficial committed
54
            down.set(address=jsonData['address'], ip=ip, remarks=json.dumps(jsonData['content'], ensure_ascii=False))
wanli's avatar
wanli committed
55 56
            commit()

wanli's avatar
wanli committed
57 58 59 60 61 62 63 64 65 66 67 68
def convert_url_to_local_path(url):
    parsed_result = urlparse(url)

    target_file = os.sep.join([config.get("UPLOAD_PATH").replace('\\', '/'), parsed_result.path])
    target_file = os.path.normpath(target_file)

    return target_file

class DownloadManager(object):
    def __init__(self):
        super(DownloadManager, self).__init__()

wanli's avatar
wanli committed
69
    def add(self, user, data):
wanli's avatar
wanli committed
70
        with db_session:
wanli's avatar
wanli committed
71
            app = Apps.get(id=data.get("app"), is_delete=False)
wanli's avatar
wanli committed
72 73 74
            if not app:
                return False, "app does not found"

wanli's avatar
wanli committed
75
            editor = User.get(id=user)
wanli's avatar
wanli committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89
            if not editor:
                return False, "current user is not exists"

            data.update({
                'create_by': editor,
                'create_at': datetime.now(),
                'update_by': editor,
                'update_at': datetime.now(),
            })

            result = AppDownload(app=app, imei=data.get("imei"), **data)
            commit()
            return result, "add dictionary {}.".format("success" if result else "fail")

wanli's avatar
wanli committed
90
    def delete(self, user, uuid):
wanli's avatar
wanli committed
91
        with db_session:
wanli's avatar
wanli committed
92
            editor = User.get(id=user)
wanli's avatar
wanli committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            if not editor:
                return False, "current user is not exists"

            result = AppDownload.get(uuid=uuid)
            if not result:
                return False, "dict does not exists"

            result.set(is_delete=True, delete_at=datetime.now(), delete_by=editor)
            commit()

            return result, "delete dictionary {}.".format("success" if result else "fail")

    def get(self, data):
        # 流程如下:
        # 获取前端传来的应用UUID和IMEI
        # 根据应用UUID查找应用信息,找不到返回失败信息
        # 如果应用ID是evue_launcher,则进行如下处理
        #   遍历系统所有应用,将相关应用根据4个一组拼接到一个数组里面去
        #   最后将这些信息导出为一个JSON文件
        #   将evue_launcher.evue和evue_dock.evue以及相关资源文件进行打包
        #   以上文件全部是复制处理,最后根据格式生成文件夹,将epk文件写入到这里
        #   读取这个epk文件,以encoding='utf8'格式返回字节流
        # 否则就是普通应用
        #   查找出这个应用路径以及依赖文件
        #   将这些文件进行打包
        #   读取这个epk文件,以encoding='utf8'格式返回字节流
        # 此次下载将生成一次下载记录

        # 当前还没有校验前端传来的IMEI是否是合法的
wanli's avatar
wanli committed
122 123
        # 根据IMEI查找设备,根据设备查找用户,根据用户查找应用
        
wanliofficial's avatar
wanliofficial committed
124
        app = None
wanli's avatar
wanli committed
125
        with db_session:
wanli's avatar
wanli committed
126
            # 根据IMEI查找设备
wanli's avatar
wanli committed
127 128 129 130 131 132
            # device = Device.select().where(imei=data.get("imei")).first()
            # logger.info(data)
            # if not device:
            #     return False, "device not found"
            # if not device.create_by:
            #     return False, "create user is null"
wanliofficial's avatar
wanliofficial committed
133

wanli's avatar
wanli committed
134 135 136
            app = Apps.select(app_name=data.get("id")).order_by(desc(Apps.create_at))
            if len(app) > 1:
                app = Apps.select(lambda p: p.app_name == data.get("id") and p.create_by.account == 'evm').order_by(desc(Apps.create_at))
wanliofficial's avatar
wanliofficial committed
137

wanli's avatar
wanli committed
138 139 140
            if not app:
                return False, "app not found"

wanli's avatar
wanli committed
141
            app = app[:]
wanli's avatar
wanli committed
142
            epk_path = ""
wanli's avatar
wanli committed
143 144
            if app[0].app_build_log:
                epk_path = os.sep.join([os.getcwd(), app[0].app_build_log.app_path])
wanli's avatar
wanli committed
145 146
                if not os.path.exists(epk_path):
                    return False, "epk file not found"
wanliofficial's avatar
wanliofficial committed
147

wanli's avatar
wanli committed
148 149 150
                down = AppDownload(app=app[0], imei=data.get("imei"))
                commit()
                if down:
wanliofficial's avatar
wanliofficial committed
151 152
                    real_ip = flask_request.headers['X-Forwarded-For']
                    update_download_information(real_ip, down.id)
wanliofficial's avatar
wanliofficial committed
153

wanli's avatar
wanli committed
154
            return epk_path, "get dictionary {}.".format("success" if epk_path else "no data")
wanli's avatar
wanli committed
155

wanli's avatar
wanli committed
156
    def getList(self, user, data):
wanli's avatar
wanli committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
        if not data or len(data) <= 0:
            return False, 0, "parameters can not be null."

        temp = copy.deepcopy(data)
        if 'pagenum' in temp:
            temp.pop('pagenum')
        if 'pagesize' in temp:
            temp.pop('pagesize')
        if 'scope_type' in temp:
            temp.pop('scope_type')
        temp.setdefault("is_delete", False)

        with db_session:
            if "scope_type" in data and data.get("scope_type") == "list":
                result = AppDownload.select().where(**temp).order_by(AppDownload.sort)
                temp = []
                for item in result:
                    temp.append(item.to_dict(only=["uuid"]))
                return temp, len(temp), "get dictionary list {}.".format("success" if temp else "no data")

            result = AppDownload.select().where(**temp).order_by(desc(AppDownload.download_at)).page(data.get("pagenum", 1), data.get("pagesize", 10))
            count = AppDownload.select().where(**temp).count()

            if result:
                temp = []
                for item in result:
                    t = item.to_dict(with_collections=True, related_objects=True)
                    t.update({
                        "app": item.app.to_dict(exclude=["create_by", "update_by", "delete_by", "is_delete"]),
                        "download_at": item.download_at.strftime("%Y-%m-%d %H:%M:%S")
                    })
                    temp.append(t)
                result = temp

            return result, count, "get dictionary {}.".format("success" if result else "fail")

wanli's avatar
wanli committed
193
    def update(self, user, uuid, data):
wanli's avatar
wanli committed
194 195 196 197 198 199
        # 当参数为空时,直接返回错误
        if len(data) <= 0 or (len(data.keys()) == 1 and "id" in data):
            return False, "parameters can not be null."

        with db_session:
            # 查询请求者是否存在
wanli's avatar
wanli committed
200
            editor = User.get(id=user)
wanli's avatar
wanli committed
201 202 203 204 205 206 207 208 209 210 211 212 213
            if not editor:
                return False, "current user is not exists"

            result = AppDownload.get(uuid=uuid)
            if not result:
                return False, "app does not found"

            result.set(update_at=datetime.now(), update_by=editor, **data)
            commit()

            return result, "update dictionary {}.".format("success" if result else "fail")

downloadManager = DownloadManager()