Statistics
| Branch: | Tag: | Revision:

root / snf-pithos-app / pithos / api / public.py @ b2d77f0e

History | View | Annotate | Download (6.2 kB)

1 2e662088 Antony Chazapis
# Copyright 2011-2012 GRNET S.A. All rights reserved.
2 f7b1d2af Antony Chazapis
# 
3 f7b1d2af Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 f7b1d2af Antony Chazapis
# without modification, are permitted provided that the following
5 f7b1d2af Antony Chazapis
# conditions are met:
6 f7b1d2af Antony Chazapis
# 
7 f7b1d2af Antony Chazapis
#   1. Redistributions of source code must retain the above
8 f7b1d2af Antony Chazapis
#      copyright notice, this list of conditions and the following
9 f7b1d2af Antony Chazapis
#      disclaimer.
10 f7b1d2af Antony Chazapis
# 
11 f7b1d2af Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 f7b1d2af Antony Chazapis
#      copyright notice, this list of conditions and the following
13 f7b1d2af Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 f7b1d2af Antony Chazapis
#      provided with the distribution.
15 f7b1d2af Antony Chazapis
# 
16 f7b1d2af Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 f7b1d2af Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 f7b1d2af Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 f7b1d2af Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 f7b1d2af Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 f7b1d2af Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 f7b1d2af Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 f7b1d2af Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 f7b1d2af Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f7b1d2af Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 f7b1d2af Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 f7b1d2af Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 f7b1d2af Antony Chazapis
# 
29 f7b1d2af Antony Chazapis
# The views and conclusions contained in the software and
30 f7b1d2af Antony Chazapis
# documentation are those of the authors and should not be
31 f7b1d2af Antony Chazapis
# interpreted as representing official policies, either expressed
32 f7b1d2af Antony Chazapis
# or implied, of GRNET S.A.
33 f7b1d2af Antony Chazapis
34 f7b1d2af Antony Chazapis
import logging
35 f7b1d2af Antony Chazapis
36 f7b1d2af Antony Chazapis
from django.http import HttpResponse
37 8fbfd37b Antony Chazapis
from django.views.decorators.csrf import csrf_exempt
38 f7b1d2af Antony Chazapis
39 6e147ecc Antony Chazapis
from synnefo.lib.astakos import get_user
40 cf441bae Antony Chazapis
41 7bef5750 Antony Chazapis
from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
42 83b4c5fa Antony Chazapis
from pithos.api.util import (put_object_headers, update_manifest_meta,
43 8cb45c13 Antony Chazapis
    validate_modification_preconditions, validate_matching_preconditions,
44 8cb45c13 Antony Chazapis
    object_data_response, api_method)
45 bb4eafc6 Antony Chazapis
from pithos.api.short_url import decode_url
46 cf441bae Antony Chazapis
from pithos.api.settings import AUTHENTICATION_URL, AUTHENTICATION_USERS
47 f7b1d2af Antony Chazapis
48 f7b1d2af Antony Chazapis
49 f7b1d2af Antony Chazapis
logger = logging.getLogger(__name__)
50 f7b1d2af Antony Chazapis
51 f7b1d2af Antony Chazapis
52 8fbfd37b Antony Chazapis
@csrf_exempt
53 bb4eafc6 Antony Chazapis
def public_demux(request, v_public):
54 cf441bae Antony Chazapis
    get_user(request, AUTHENTICATION_URL, AUTHENTICATION_USERS)
55 f7b1d2af Antony Chazapis
    if request.method == 'HEAD':
56 bb4eafc6 Antony Chazapis
        return public_meta(request, v_public)
57 f7b1d2af Antony Chazapis
    elif request.method == 'GET':
58 bb4eafc6 Antony Chazapis
        return public_read(request, v_public)
59 f7b1d2af Antony Chazapis
    else:
60 f7b1d2af Antony Chazapis
        return method_not_allowed(request)
61 f7b1d2af Antony Chazapis
62 297513ba Antony Chazapis
@api_method('HEAD', user_required=False)
63 bb4eafc6 Antony Chazapis
def public_meta(request, v_public):
64 f7b1d2af Antony Chazapis
    # Normal Response Codes: 204
65 08de868d Antony Chazapis
    # Error Response Codes: internalServerError (500),
66 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
67 f7b1d2af Antony Chazapis
    #                       badRequest (400)
68 f7b1d2af Antony Chazapis
    
69 f7b1d2af Antony Chazapis
    try:
70 bb4eafc6 Antony Chazapis
        v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
71 bb4eafc6 Antony Chazapis
                                                    decode_url(v_public))
72 61efb530 Antony Chazapis
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
73 808cea65 Antony Chazapis
                                                    v_container, v_object, 'pithos')
74 61efb530 Antony Chazapis
        public = request.backend.get_object_public(request.user_uniq, v_account,
75 39593b2b Giorgos Verigakis
                                                    v_container, v_object)
76 cca6c617 Antony Chazapis
    except:
77 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
78 f7b1d2af Antony Chazapis
    
79 e0f916bb Antony Chazapis
    if not public:
80 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
81 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
82 f7b1d2af Antony Chazapis
    
83 4a1c29ea Antony Chazapis
    response = HttpResponse(status=200)
84 4a1c29ea Antony Chazapis
    put_object_headers(response, meta, True)
85 f7b1d2af Antony Chazapis
    return response
86 f7b1d2af Antony Chazapis
87 297513ba Antony Chazapis
@api_method('GET', user_required=False)
88 bb4eafc6 Antony Chazapis
def public_read(request, v_public):
89 f7b1d2af Antony Chazapis
    # Normal Response Codes: 200, 206
90 08de868d Antony Chazapis
    # Error Response Codes: internalServerError (500),
91 f7b1d2af Antony Chazapis
    #                       rangeNotSatisfiable (416),
92 f7b1d2af Antony Chazapis
    #                       preconditionFailed (412),
93 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
94 f7b1d2af Antony Chazapis
    #                       badRequest (400),
95 f7b1d2af Antony Chazapis
    #                       notModified (304)
96 f7b1d2af Antony Chazapis
    
97 f7b1d2af Antony Chazapis
    try:
98 bb4eafc6 Antony Chazapis
        v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
99 bb4eafc6 Antony Chazapis
                                                    decode_url(v_public))
100 61efb530 Antony Chazapis
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
101 808cea65 Antony Chazapis
                                                    v_container, v_object, 'pithos')
102 61efb530 Antony Chazapis
        public = request.backend.get_object_public(request.user_uniq, v_account,
103 39593b2b Giorgos Verigakis
                                                    v_container, v_object)
104 cca6c617 Antony Chazapis
    except:
105 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
106 f7b1d2af Antony Chazapis
    
107 e0f916bb Antony Chazapis
    if not public:
108 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
109 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
110 f7b1d2af Antony Chazapis
    
111 f7b1d2af Antony Chazapis
    # Evaluate conditions.
112 f7b1d2af Antony Chazapis
    validate_modification_preconditions(request, meta)
113 f7b1d2af Antony Chazapis
    try:
114 f7b1d2af Antony Chazapis
        validate_matching_preconditions(request, meta)
115 f7b1d2af Antony Chazapis
    except NotModified:
116 f7b1d2af Antony Chazapis
        response = HttpResponse(status=304)
117 4a1c29ea Antony Chazapis
        response['ETag'] = meta['ETag']
118 f7b1d2af Antony Chazapis
        return response
119 f7b1d2af Antony Chazapis
    
120 8cb45c13 Antony Chazapis
    sizes = []
121 8cb45c13 Antony Chazapis
    hashmaps = []
122 8cb45c13 Antony Chazapis
    if 'X-Object-Manifest' in meta:
123 8cb45c13 Antony Chazapis
        try:
124 6d817842 Antony Chazapis
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
125 61efb530 Antony Chazapis
            objects = request.backend.list_objects(request.user_uniq, v_account,
126 39593b2b Giorgos Verigakis
                                src_container, prefix=src_name, virtual=False)
127 cca6c617 Antony Chazapis
        except:
128 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
129 8cb45c13 Antony Chazapis
        
130 8cb45c13 Antony Chazapis
        try:
131 8cb45c13 Antony Chazapis
            for x in objects:
132 61efb530 Antony Chazapis
                s, h = request.backend.get_object_hashmap(request.user_uniq,
133 39593b2b Giorgos Verigakis
                                        v_account, src_container, x[0], x[1])
134 8cb45c13 Antony Chazapis
                sizes.append(s)
135 8cb45c13 Antony Chazapis
                hashmaps.append(h)
136 cca6c617 Antony Chazapis
        except:
137 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
138 8cb45c13 Antony Chazapis
    else:
139 8cb45c13 Antony Chazapis
        try:
140 61efb530 Antony Chazapis
            s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
141 39593b2b Giorgos Verigakis
                                                        v_container, v_object)
142 8cb45c13 Antony Chazapis
            sizes.append(s)
143 8cb45c13 Antony Chazapis
            hashmaps.append(h)
144 cca6c617 Antony Chazapis
        except:
145 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
146 f7b1d2af Antony Chazapis
    
147 bb4eafc6 Antony Chazapis
    if 'Content-Disposition' not in meta:
148 bb4eafc6 Antony Chazapis
        name = v_object.rstrip('/').split('/')[-1]
149 bb4eafc6 Antony Chazapis
        if not name:
150 bb4eafc6 Antony Chazapis
            name = v_public
151 bb4eafc6 Antony Chazapis
        meta['Content-Disposition'] = 'attachment; filename=%s' % (name,)
152 bb4eafc6 Antony Chazapis
    
153 8cb45c13 Antony Chazapis
    return object_data_response(request, sizes, hashmaps, meta, True)
154 f7b1d2af Antony Chazapis
155 297513ba Antony Chazapis
@api_method(user_required=False)
156 83b4c5fa Antony Chazapis
def method_not_allowed(request, **v_args):
157 f7b1d2af Antony Chazapis
    raise ItemNotFound('Object does not exist')