Beispiel #1
0
def download_ipa(uuid: str, redirect: bool = False, download_id: str = ""):
    _user = UserInfo.objects.get(uuid=uuid)
    _info = IosAccountInfo.objects.get(account=_user.account)

    filepath = "income/%s/%s_%s.ipa" % (_user.project, _info.team_id,
                                        _info.devices_num)
    if redirect:
        return HttpResponseRedirect(static_entry(filepath))
    else:
        if not download_id:
            download_id = random_str(32)
        __download_process[download_id] = 0
        __download_total[download_id] = os.path.getsize(
            os.path.join("static", filepath))

        def file_iterator(chunk_size=4 * 1024):
            with open(os.path.join("static", filepath), mode="rb") as fin:
                num = 0
                while True:
                    # todo: 不允许相同的多个下载任务

                    c = fin.read(chunk_size)
                    num += len(c)
                    __download_process[download_id] = num
                    if c:
                        yield c
                    else:
                        break

        rsp = StreamingHttpResponse(file_iterator())
        rsp.set_signed_cookie("download_id", download_id, salt="zhihu")
        return rsp