Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add upload endpoint #533

Merged
merged 1 commit into from
Jul 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion quetz/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# Distributed under the terms of the Modified BSD License.
import asyncio
import datetime
import hashlib
import json
import logging
import os
Expand All @@ -12,7 +13,7 @@
from concurrent.futures import ThreadPoolExecutor
from contextlib import contextmanager
from email.utils import formatdate
from tempfile import SpooledTemporaryFile
from tempfile import SpooledTemporaryFile, TemporaryFile
from typing import List, Optional, Tuple, Type

import pydantic
Expand Down Expand Up @@ -1288,6 +1289,77 @@ def post_file_to_package(
dao.update_channel_size(package.channel_name)


@api_router.post(
"/channels/{channel_name}/upload/{filename}", status_code=201, tags=["upload"]
)
async def post_upload(
request: Request,
channel_name: str,
filename: str,
sha256: str,
force: bool = False,
dao: Dao = Depends(get_dao),
auth: authorization.Rules = Depends(get_rules),
):
logger.debug(
f"Uploading file {filename} with checksum {sha256} to channel {channel_name}"
)

upload_hash = hashlib.sha256()

body = TemporaryFile()
async for chunk in request.stream():
body.write(chunk)
upload_hash.update(chunk)

if sha256 and upload_hash.hexdigest() != sha256:
raise HTTPException(
status_code=status.HTTP_406_NOT_ACCEPTABLE, detail="Wrong SHA256 checksum"
)

user_id = auth.assert_user()
auth.assert_create_package(channel_name)
condainfo = CondaInfo((body), filename)
dest = os.path.join(condainfo.info["subdir"], filename)

pkgstore.add_package(body, channel_name, dest)

package_name = str(condainfo.info.get("name"))
package_data = rest_models.Package(
name=package_name,
summary=str(condainfo.about.get("summary", "n/a")),
description=str(condainfo.about.get("description", "n/a")),
)
if not dao.get_package(channel_name, package_name):
dao.create_package(
channel_name,
package_data,
user_id,
authorization.OWNER,
)

try:
version = dao.create_version(
channel_name=channel_name,
package_name=package_name,
package_format=condainfo.package_format,
platform=condainfo.info["subdir"],
version=condainfo.info["version"],
build_number=condainfo.info["build_number"],
build_string=condainfo.info["build"],
size=condainfo.info["size"],
filename=filename,
info=json.dumps(condainfo.info),
uploader_id=user_id,
upsert=force,
)
except IntegrityError:
logger.debug(f"duplicate package '{package_name}' in channel '{channel_name}'")
raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail="Duplicate")

pm.hook.post_add_package_version(version=version, condainfo=condainfo)


@api_router.post("/channels/{channel_name}/files/", status_code=201, tags=["files"])
def post_file_to_channel(
background_tasks: BackgroundTasks,
Expand Down
4 changes: 2 additions & 2 deletions quetz/pkgstores.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from contextlib import contextmanager
from os import PathLike
from threading import Lock
from typing import IO, BinaryIO, List, NoReturn, Tuple, Union
from typing import IO, List, NoReturn, Tuple, Union

import fsspec
from tenacity import retry, retry_if_exception_type, stop_after_attempt
Expand All @@ -29,7 +29,7 @@

from quetz.errors import ConfigError

File = BinaryIO
File = IO[bytes]

StrPath = Union[str, PathLike]

Expand Down