Пример #1
0
async def handler(request):
    user_id = request.path_params['id']
    isOsu = request.headers.get("user-agent", "") == "osu!"

    # Let check is gif avatar?
    if os.path.isfile(f"{config['avatar_dir']}/{user_id}.png"):
        # Okay picture, i send you to user!
        return FileResponse(f"{config['avatar_dir']}/{user_id}.png")

    if os.path.isfile(f"{config['avatar_dir']}/{user_id}.gif"):
        if isOsu:
            # It's osu send to client one thing FIRST FRAME OF GIF
            gif_object = Image.open(f"{config['avatar_dir']}/{user_id}.gif")
            buffer = io.BytesIO()
            if gif_object.is_animated:
                gif_object.seek(0)
                gif_object.save(buffer, format="PNG")
                buffer.seek(0)
            else:
                # Fake gif, or not animated ;d
                gif_object.save(buffer, format="PNG")
                buffer.seek(0)

            return StreamingResponse(buffer, media_type="image/png")
        else:
            # It's just a website or another source
            return FileResponse(f"{config['avatar_dir']}/{user_id}.gif")
    
    # We are not found anything, than send default 999.png!
    return FileResponse(f"{config['avatar_dir']}/0.png")
Пример #2
0
def static(request: Request):
    resource = request.path_params.get("resource", "")
    if "." not in resource:
        return FileResponse(path=os.path.join("build", "index.html"))
    elif "/static" in resource:
        index = resource.index("/static")
        path = resource[index:]
        return FileResponse(path=os.path.join("build", path))
    else:
        return FileResponse(path=os.path.join("build", resource))
Пример #3
0
Файл: main.py Проект: hill/UEM2
def read_index(request: Request):
    # check first if requested file exists
    path = request.path_params["catchall"]
    file = config.FRONTEND_LOC + "/" + path

    if os.path.exists(file):
        return FileResponse(file)

    # otherwise return index files
    index = config.FRONTEND_LOC + "/index.html"
    return FileResponse(index)
Пример #4
0
async def get_default_submission(
    extension: Literal["tar", "zip"]
):  # user: User = Security(get_current_user, scopes=["submission.get_default"])):
    media = 'application/octet-stream'
    file = "default_ai"
    if extension == "tar":
        return FileResponse(DEFAULT_SUBMISSION_TAR_PATH,
                            media_type=media,
                            filename=file + ".tar")
    return FileResponse(DEFAULT_SUBMISSION_ZIP_PATH,
                        media_type=media,
                        filename=file + ".zip")
Пример #5
0
 def get(file_id: str) -> Awaitable[FileResponse]:
     with create_session() as session:
         try:
             file: Document = session.query(Document).filter(
                 Document.uuid == file_id).one()
         except NoResultFound:
             raise DALError(HTTPStatus.NOT_FOUND.value)
         if file.type == 'contract':
             file = cast(Contract, file)
             return FileResponse(file.path_to_document,
                                 filename=file.title +
                                 '.pdf')  # type: ignore
         return FileResponse(file.path_to_document)  # type: ignore
Пример #6
0
def get_file(hashed_code):
    import glob
    import os

    paths = glob.glob('./temp_files/*')
    for path in paths:
        if hashed_code in path and os.path.isdir(path):
            FileController.write_data(''.join(open(f'./temp_files/{hashed_code}_exported.xml')).strip(),
                                      path.replace('./temp_files\\', ''))
            return FileResponse(path=f'./temp_files/{hashed_code}.zip', filename=f'./temp_files/{hashed_code}.zip')

    import aiofiles
    return FileResponse(path=f'./temp_files/{hashed_code}_exported.xml',
                        filename=f'./temp_files/{hashed_code}_exported.xml')
Пример #7
0
async def index_html(rest_of_path: str):
    """
    Emulate path fallback to index.html.
    """
    if rest_of_path.startswith('api') or rest_of_path.startswith('.'):
        raise HTTPException(status_code=404, detail="Not Found")
    uibase = Path(__file__).parent / 'ui/installed/'
    if (uibase / rest_of_path).is_file():
        return FileResponse(str(uibase / rest_of_path))

    index_file = uibase / 'index.html'
    if not index_file.is_file():
        return FileResponse(str(uibase.parent / 'fallback_file.html'))
    # Fall back to index.html, as indicated by vue router docs
    return FileResponse(str(index_file))
Пример #8
0
async def convert(request: Request,
                  file: UploadFile = File(...),
                  fileType: str = Path(
                      ..., description="目标文件类型,支持:doc、docx、rtf、html、pdf、xml")):
    if fileType in formats:
        file_name = str(uuid.uuid1())
        path = os.path.join(base_path, file_name)
        os.makedirs(base_path, exist_ok=True)
        contents = await file.read()
        with open(path, 'wb') as f:
            f.write(contents)
        global docs
        try:
            hr, doc = docs.Open(path, ReadOnly=True)
            if hr != S_OK:
                docs = init(True)
                hr, doc = docs.Open(path, ReadOnly=True)
                if hr != S_OK:
                    raise ConvertException("Can't open file in path", hr)
            out_dir = base_path + "/out"
            os.makedirs(out_dir, exist_ok=True)
            new_path = out_dir + "/" + file_name + "." + fileType
            doc.SaveAs2(new_path, FileFormat=formats[fileType])
            doc.Close(wpsapi.wdDoNotSaveChanges)
            return FileResponse(new_path, filename=file_name + "." + fileType)
        except ConvertException as e:
            print(e)
            return JSONResponse(status_code=500, content=str(e))
    else:
        return JSONResponse(status_code=500,
                            content=str("格式类型转换暂不支持:" + fileType))
Пример #9
0
def stylecloud(request: StyleCloudRequest):
    params = request.dict()
    url = params.pop("url", None)
    text = params.pop("text", None)
    background_color = params.pop("background_color", None)
    gradient = params.pop("gradient", None)

    if gradient == Gradient.none:
        gradient = None

    if url is not None:
        require_diffbot()
        # TODO: replace this with a more lightweight alternative than diffbot
        result = article(
            url,
            token=DIFFBOT_TOKEN,
            paging=False,
            discussion=False,
            maxTags=0,
            norender=True,
        )["objects"][0]
        pprint.pprint(result)
        text = result["text"]
    elif text is None:
        raise Exception('Must provide either "text" or "url".')

    sc.gen_stylecloud(**params,
                      text=text,
                      gradient=gradient,
                      icon_dir="/tmp/icons",
                      output_name=OUTPUT_NAME,
                      background_color=background_color.as_hex())

    return FileResponse(OUTPUT_NAME, media_type="image/png", headers=headers)
Пример #10
0
async def download_artifact(request: Request) -> Response:
    run_uuid = request.path_params["run_uuid"]
    filepath = request.query_params.get("path", "")
    stream = to_bool(request.query_params.get("stream"), handle_none=True)
    force = to_bool(request.query_params.get("force"), handle_none=True)
    render = to_bool(request.query_params.get("render"), handle_none=True)
    if not filepath:
        return Response(
            content="A `path` query param is required to stream a file content",
            status_code=status.HTTP_400_BAD_REQUEST,
        )
    if render and not filepath.endswith(".ipynb"):
        return Response(
            content=
            "Artifact with 'filepath={}' does not have a valid extension.".
            format(filepath),
            status_code=status.HTTP_400_BAD_REQUEST,
        )
    subpath = "{}/{}".format(run_uuid, filepath).rstrip("/")
    archived_path = await download_file(subpath=subpath, check_cache=not force)
    if not archived_path:
        return Response(
            content="Artifact not found: filepath={}".format(archived_path),
            status_code=status.HTTP_404_NOT_FOUND,
        )
    if stream:
        if render:
            archived_path = render_notebook(archived_path,
                                            check_cache=not force)
        return FileResponse(archived_path)
    return redirect(archived_path)
Пример #11
0
def uploads(request):
    archive_path = shutil.make_archive(path / 'uploads', 'zip',
                                       path / 'images')
    print(archive_path)

    response = FileResponse(archive_path, media_type='application/zip')
    return response
Пример #12
0
async def http_exception(request, exc):
    url = urlparse(str(request.url))
    if not url.path.startswith("/api/v1"):
        if (Path(webui_dir("dist")).is_dir()
                and Path(webui_dir(join("dist", "index.html"))).is_file()):
            return FileResponse(webui_dir(join("dist", "index.html")))
    return JSONResponse({"detail": "Not Found"}, status_code=404)
Пример #13
0
async def download(filename: str):
    """ send a whole file """
    target_file = settings.folder + filename
    if not isfile(target_file):
        raise HTTPException(status_code=404, detail="Item not found")
    response = FileResponse(path=settings.folder + filename, filename=filename)
    return response
Пример #14
0
async def download_asset(genome: str, asset: str, tag: str = None):
    """
    Returns an archive. Requires the genome name and the asset name as an input.

    Since the refgenconf.RefGenConf object structure has changed (tags were introduced),
    the default tag has to be selected behind the scenes
    """
    tag = tag or rgc.get_default_tag(
        genome, asset
    )  # returns 'default' for nonexistent genome/asset; no need to catch
    file_name = "{}__{}{}".format(asset, tag, ".tgz")
    path, remote = get_datapath_for_genome(
        rgc,
        dict(
            genome=rgc.get_genome_alias(digest=genome, fallback=True),
            file_name=file_name,
        ),
        remote_key="http",
    )
    _LOGGER.info("file source: {}".format(path))
    if remote:
        _LOGGER.info("redirecting to URL: '{}'".format(path))
        return RedirectResponse(path)
    _LOGGER.info("serving asset file: '{}'".format(path))
    if os.path.isfile(path):
        return FileResponse(path,
                            filename=file_name,
                            media_type="application/octet-stream")
    else:
        msg = MSG_404.format("asset ({})".format(asset))
        _LOGGER.warning(msg)
        raise HTTPException(status_code=404, detail=msg)
Пример #15
0
async def get(request):
    """
    Implement GET for download.
    :param request: (starlette.requests.Request)
    :return: (starlette.Response.FileResponse)
    """
    # retrieve unique id
    uuid_ = request.path_params['id_']
    if len(uuid_) != 36: return error_response('uuid is ot valid', 404)

    # retrieve record
    from src.models import select_from_index
    cursor = select_from_index(uuid_)
    count = 0
    name = None
    for r in cursor:
        logger.debug(r)
        id_, name, uuid_ = r
        count += 1
        break
    if count != 1: return error_response('Wrong unique id', 404)

    # parse file data
    filename = f'{uuid_}{SEPARATOR}{name}'
    path_ = os.path.join(FS_DIR, filename)
    ext = name.split('.')[1]

    from starlette.responses import FileResponse
    return FileResponse(path_, media_type=f"image/{ext}", filename=filename)
Пример #16
0
def get_diff_by_id(response: Response,
                   diff_id: str = Path(..., title="Diff Id to return"),
                   img: Optional[bool] = True):
    working_dir = BASE_WORKING_DIR + "/" + diff_id

    json_path = working_dir + "/" + DIFF_JSON
    json_exists = os.path.exists(json_path)

    pdf_path = working_dir + "/" + DIFF_PDF
    pdf_exists = os.path.exists(pdf_path)
    if (not json_exists and not pdf_exists):
        raise HTTPException(
            status_code=404,
            detail="Diff Id not found. Consider creating a new Diff Id.")

    # If only JSON exists, generate the PDF and return it, else return PDF directly
    if json_exists and not pdf_exists:
        import json
        with open(json_path, 'r') as json_file:
            changes = json.load(json_file)
        render_changes(changes, pdf_path)

    if img:
        return FileResponse(pdf_path,
                            media_type="application/pdf",
                            filename='diff.pdf')
    else:
        import json
        with open(json_path, 'r') as json_file:
            changes = json.load(json_file)
        return changes
Пример #17
0
def pdf_diff(response: Response,
             prev: UploadFile = File(...),
             current: UploadFile = File(...),
             img: Optional[bool] = True):
    diff_id = str(uuid.uuid4())
    working_dir = BASE_WORKING_DIR + "/" + diff_id + "/"
    # Create a new directory structure for each request to store the uploaded files and diff.pdf
    os.makedirs(working_dir)
    prev_path = copy_file(working_dir, prev, 'prev.pdf')
    current_path = copy_file(working_dir, current, 'current.pdf')
    changes = command_line.compute_changes(prev_path, current_path)

    json_path = working_dir + "/" + DIFF_JSON
    import json
    with open(json_path, 'w') as fp:
        json.dump(changes, fp)

    pdf_path = working_dir + "/" + DIFF_PDF
    render_changes(changes, pdf_path)
    if img:
        custom_headers = {
            DIFF_ID_HEADER: diff_id,
            'access-control-expose-headers': DIFF_ID_HEADER
        }
        return FileResponse(pdf_path,
                            media_type="application/pdf",
                            headers=custom_headers,
                            filename='diff.pdf')
    else:
        response.headers['access-control-expose-headers'] = DIFF_ID_HEADER
        response.headers[DIFF_ID_HEADER] = diff_id
        return changes
Пример #18
0
def download_file(file_id):
    file_to_download = drive.CreateFile({'id': file_id})
    os.chdir("download")
    file_to_download.GetContentFile(str(file_to_download['title']))
    os.chdir("..")
    return file_to_download['title']
    return FileResponse(str(file_to_download['title']))
Пример #19
0
async def download_asset(genome: str, asset: str, tag: str = None):
    """
    Returns an archive. Requires the genome name and the asset name as an input.

    Optionally, 'tag' query parameter can be specified to get a tagged asset archive. Default tag is returned otherwise.
    """
    tag = tag or rgc.get_default_tag(
        genome, asset
    )  # returns 'default' for nonexistent genome/asset; no need to catch
    file_name = "{}__{}{}".format(asset, tag, ".tgz")
    path, remote = get_datapath_for_genome(
        rgc,
        dict(
            genome=rgc.get_genome_alias(digest=genome, fallback=True),
            file_name=file_name,
        ),
        remote_key="http",
    )
    _LOGGER.info("file source: {}".format(path))
    if remote:
        _LOGGER.info("redirecting to URL: '{}'".format(path))
        return RedirectResponse(path)
    _LOGGER.info("serving asset file: '{}'".format(path))
    if os.path.isfile(path):
        return FileResponse(path,
                            filename=file_name,
                            media_type="application/octet-stream")
    else:
        msg = MSG_404.format("asset ({})".format(asset))
        _LOGGER.warning(msg)
        raise HTTPException(status_code=404, detail=msg)
Пример #20
0
def test_file_response_with_missing_file_raises_error(tmpdir):
    path = os.path.join(tmpdir, "404.txt")
    app = FileResponse(path=path, filename="404.txt")
    client = TestClient(app)
    with pytest.raises(RuntimeError) as exc_info:
        client.get("/")
    assert "does not exist" in str(exc_info.value)
Пример #21
0
async def image_endpoint(img: str):
    try:
        print("load img:", img)
        return FileResponse(img, media_type="image/jpg")
    except Exception as e:
        logging.error(e)
        return None, 200
Пример #22
0
def proxysh():
    alter("app/freelan/freeproxy.sh", "app/freelan/new-freeproxy.sh",
          "freelan.passphrase", app.passphrase)
    response = FileResponse("app/freelan/new-freeproxy.sh")
    response.media_type = "application/octet-stream"
    response.filename = "freeproxy.sh"
    return response
async def get_registration_event_payment(request: Request,
                                         event_registration_id: int):
    event_registration_controller = EventRegistrationController(
        request=request)
    event_data = event_registration_controller.get_event_registration_by_id(
        registration_event_id=event_registration_id)
    return FileResponse(event_data.payment_file)
Пример #24
0
async def create_stream(batch_using: bool = True,
                        transform_flag: bool = True,
                        include_spam_trash: bool = False,
                        max_results: int = 25,
                        max_workers: int = 25,
                        file_return: str = None):
    """
    create_stream: 

    Args:
    
        batch_using (bool, optional): if Flag False -> Take nextPageToken else True -> Take by batch. Defaults to True.
        transform_flag (bool, optional): Using this flag if you want used Transformer Model. Defaults to True.
        include_spam_trash (bool, optional): Include messages from SPAM and TRASH in the results.. Defaults to False.
        max_results (int, optional): Maximum number of messages to return.. Defaults to 25.
        max_workers (int, optional): Maximun number of worker used by multithreading. Defaults to 25.
        file_return (str, optional): Type of file returned by API. Defaults to None.

    Raises:
    
        HTTPException: [description]

    Returns:
    
        API, Json, Csv: Return streaming data from Ika's streamer
    """

    message_id = GmailHelper("prod").get_message_id(
        "me",
        include_spam_trash=include_spam_trash,
        max_results=max_results,
        batch_using=batch_using)

    gmail_collection = mdb["streamer"]
    gmail_collection.insert(
        CollecterModel("prod", transform_flag=transform_flag).collect_mail(
            user_id="me", message_id=message_id, max_workers=max_workers))

    response = gmail_collection.find_one()

    if not response:
        raise HTTPException(status_code=404, detail="Cast not found")

    if file_return is None:
        return RedirectResponse(
            "http://127.0.0.1:8004/api/v1/classifier/labelling/build")

    elif file_return == 'csv':
        data = pd.DataFrame(list(gmail_collection.find()))
        compression_opts = dict(method='zip', archive_name='out.csv')
        data.to_csv('gmail_file.zip',
                    index=False,
                    compression=compression_opts)
        file_location = 'gmail_file.zip'
        return FileResponse(file_location,
                            media_type='application/octet-stream',
                            filename='gmail_file.zip')

    elif file_return == 'json':
        pass
Пример #25
0
async def get_person_data(id, ext):
    person = await retrieve_person(id)
    if person:
        read_and_interpolate_file('./carta_agradecimiento.txt', person, ext)
        cwd = os.getcwd()
        return FileResponse("{}/static/{}.{}".format(cwd,id,ext))
    return ErrorResponseModel("An error occurred.", 404, "Person doesn't exist.")
Пример #26
0
async def get_image(filename: str):
    filepath = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                            "images", filename)
    if not os.path.exists(filepath):
        raise HTTPException(status_code=404, detail="Image not found")

    return FileResponse(filepath)
Пример #27
0
async def analyze(request):
    data = await request.form()
    img_bytes = await (data['file'].read())
    img = open_image(BytesIO(img_bytes))
    p_img = unetCountEggs(learn, img.data.numpy().transpose(1,2,0))  
    PIL.Image.fromarray((255*p_img).astype(np.uint8)).save(f'app/data/p_img.jpg')
    return(FileResponse('app/data/p_img.jpg'))
Пример #28
0
def export_upload(
        background: BackgroundTasks,
        path: Path = Depends(imagepath_parameter),
):
    """
    Export the upload representation of an image.
    """
    image = path.get_original()
    check_representation_existence(image)

    upload_file = image.get_upload().resolve()
    media_type = image.media_type
    if upload_file.is_dir():
        # if archive has been deleted
        tmp_export = Path(f"/tmp/{unique_name_generator()}")
        make_zip_archive(tmp_export, upload_file)

        def cleanup(tmp):
            tmp.unlink(missing_ok=True)

        background.add_task(cleanup, tmp_export)
        upload_file = tmp_export
        media_type = "application/zip"

    return FileResponse(upload_file, media_type=media_type, filename=path.name)
Пример #29
0
def gostsh():
    alter("app/gost/gostproxy.sh", "app/gost/new-gostproxy.sh",
          "gostpassword.passphrase", app.gostpassword)
    response = FileResponse("app/gost/new-gostproxy.sh")
    response.media_type = "application/octet-stream"
    response.filename = "gostproxy.sh"
    return response
Пример #30
0
async def analyze(request):
    print('analyze begin')
    data = await request.form()
    content = await (data['file'].read())
    print(data['file'])
    print('asdf')
    print(content)
    s = str(content, 'utf-8')
    data = StringIO(s)
    df = pd.read_csv(data)
    learn = load_learner(path / export_file_name)
    # if we want to do GPU:
    # learn.model = learn.model.cuda()
    #dl = learn.dls.train_dl.new(df) #original
    dl = learn.dls.test_dl(df)
    _, __, y = learn.get_preds(dl=dl, with_decoded=True)
    print(y)
    #y is a tensor, convert to numpy
    preds = y.numpy()
    print(preds)
    print(preds.shape)
    df['predictions'] = preds
    #df = pd.concat([df, pd.DataFrame(preds)], axis=1)
    df.info()
    print('add preds')
    path_res = Path('app/static/')
    print(path_res)
    df.to_csv(path_res / 'results.csv', index=False)
    print('inference done')
    #return FileResponse(path_res/'results.csv', media_type='csv')
    #return FileResponse(f'{path_res}/results.csv', media_type='csv')
    return FileResponse('app/static/results.csv', media_type='csv')