aboutsummaryrefslogtreecommitdiff
path: root/license_protected_downloads/api/v3.py
blob: 24a0bc3c43cccc44a8376e2adca4fca7f8897a59 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
from django.conf import settings
from django.http import HttpResponse
from django.views.decorators.csrf import csrf_exempt

from license_protected_downloads.artifact.s3 import S3Artifact
from license_protected_downloads.api.v1 import (
    HttpResponseError,
)
from license_protected_downloads.api import v2
from license_protected_downloads.models import (
    APILog,
)


# no changes required for tokens in v3
token = v2.token


class PublishResource(v2.PublishResource):
    def __init__(self, request, path):
        super(PublishResource, self).__init__(request, path)

    def POST(self):
        b = S3Artifact.get_bucket()
        if not b:
            raise HttpResponseError('S3 is not enabled', 403)

        k = b.new_key(settings.S3_PREFIX_PATH + self.path)
        if k.exists():
            APILog.mark(self.request, 'FILE_OVERWRITE_DENIED')
            raise HttpResponseError('File already exists', 403)

        headers = {}
        mtype = self.request.POST.get('Content-Type', None)
        if mtype:
            headers['Content-Type'] = mtype

        resp = HttpResponse(status=201)
        resp['Location'] = k.generate_url(60, method='PUT', headers=headers)
        APILog.mark(self.request, 'FILE_UPLOAD', self.api_key)
        return resp


@csrf_exempt
def publish(request, path):
    return PublishResource(request, path).handle()