Statistics
| Branch: | Tag: | Revision:

root / pithos / api / public.py @ f2a3d5a3

History | View | Annotate | Download (6 kB)

1 f7b1d2af Antony Chazapis
# Copyright 2011 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 f7b1d2af Antony Chazapis
38 7bef5750 Antony Chazapis
from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
39 83b4c5fa Antony Chazapis
from pithos.api.util import (put_object_headers, update_manifest_meta,
40 8cb45c13 Antony Chazapis
    validate_modification_preconditions, validate_matching_preconditions,
41 8cb45c13 Antony Chazapis
    object_data_response, api_method)
42 bb4eafc6 Antony Chazapis
from pithos.api.short_url import decode_url
43 f7b1d2af Antony Chazapis
44 f7b1d2af Antony Chazapis
45 f7b1d2af Antony Chazapis
logger = logging.getLogger(__name__)
46 f7b1d2af Antony Chazapis
47 f7b1d2af Antony Chazapis
48 bb4eafc6 Antony Chazapis
def public_demux(request, v_public):
49 f7b1d2af Antony Chazapis
    if request.method == 'HEAD':
50 bb4eafc6 Antony Chazapis
        return public_meta(request, v_public)
51 f7b1d2af Antony Chazapis
    elif request.method == 'GET':
52 bb4eafc6 Antony Chazapis
        return public_read(request, v_public)
53 f7b1d2af Antony Chazapis
    else:
54 f7b1d2af Antony Chazapis
        return method_not_allowed(request)
55 f7b1d2af Antony Chazapis
56 297513ba Antony Chazapis
@api_method('HEAD', user_required=False)
57 bb4eafc6 Antony Chazapis
def public_meta(request, v_public):
58 f7b1d2af Antony Chazapis
    # Normal Response Codes: 204
59 f7b1d2af Antony Chazapis
    # Error Response Codes: serviceUnavailable (503),
60 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
61 f7b1d2af Antony Chazapis
    #                       badRequest (400)
62 f7b1d2af Antony Chazapis
    
63 f7b1d2af Antony Chazapis
    try:
64 bb4eafc6 Antony Chazapis
        v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
65 bb4eafc6 Antony Chazapis
                                                    decode_url(v_public))
66 61efb530 Antony Chazapis
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
67 808cea65 Antony Chazapis
                                                    v_container, v_object, 'pithos')
68 61efb530 Antony Chazapis
        public = request.backend.get_object_public(request.user_uniq, v_account,
69 39593b2b Giorgos Verigakis
                                                    v_container, v_object)
70 cca6c617 Antony Chazapis
    except:
71 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
72 f7b1d2af Antony Chazapis
    
73 e0f916bb Antony Chazapis
    if not public:
74 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
75 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
76 f7b1d2af Antony Chazapis
    
77 4a1c29ea Antony Chazapis
    response = HttpResponse(status=200)
78 4a1c29ea Antony Chazapis
    put_object_headers(response, meta, True)
79 f7b1d2af Antony Chazapis
    return response
80 f7b1d2af Antony Chazapis
81 297513ba Antony Chazapis
@api_method('GET', user_required=False)
82 bb4eafc6 Antony Chazapis
def public_read(request, v_public):
83 f7b1d2af Antony Chazapis
    # Normal Response Codes: 200, 206
84 f7b1d2af Antony Chazapis
    # Error Response Codes: serviceUnavailable (503),
85 f7b1d2af Antony Chazapis
    #                       rangeNotSatisfiable (416),
86 f7b1d2af Antony Chazapis
    #                       preconditionFailed (412),
87 f7b1d2af Antony Chazapis
    #                       itemNotFound (404),
88 f7b1d2af Antony Chazapis
    #                       badRequest (400),
89 f7b1d2af Antony Chazapis
    #                       notModified (304)
90 f7b1d2af Antony Chazapis
    
91 f7b1d2af Antony Chazapis
    try:
92 bb4eafc6 Antony Chazapis
        v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
93 bb4eafc6 Antony Chazapis
                                                    decode_url(v_public))
94 61efb530 Antony Chazapis
        meta = request.backend.get_object_meta(request.user_uniq, v_account,
95 808cea65 Antony Chazapis
                                                    v_container, v_object, 'pithos')
96 61efb530 Antony Chazapis
        public = request.backend.get_object_public(request.user_uniq, v_account,
97 39593b2b Giorgos Verigakis
                                                    v_container, v_object)
98 cca6c617 Antony Chazapis
    except:
99 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
100 f7b1d2af Antony Chazapis
    
101 e0f916bb Antony Chazapis
    if not public:
102 f7b1d2af Antony Chazapis
        raise ItemNotFound('Object does not exist')
103 8cb45c13 Antony Chazapis
    update_manifest_meta(request, v_account, meta)
104 f7b1d2af Antony Chazapis
    
105 f7b1d2af Antony Chazapis
    # Evaluate conditions.
106 f7b1d2af Antony Chazapis
    validate_modification_preconditions(request, meta)
107 f7b1d2af Antony Chazapis
    try:
108 f7b1d2af Antony Chazapis
        validate_matching_preconditions(request, meta)
109 f7b1d2af Antony Chazapis
    except NotModified:
110 f7b1d2af Antony Chazapis
        response = HttpResponse(status=304)
111 4a1c29ea Antony Chazapis
        response['ETag'] = meta['ETag']
112 f7b1d2af Antony Chazapis
        return response
113 f7b1d2af Antony Chazapis
    
114 8cb45c13 Antony Chazapis
    sizes = []
115 8cb45c13 Antony Chazapis
    hashmaps = []
116 8cb45c13 Antony Chazapis
    if 'X-Object-Manifest' in meta:
117 8cb45c13 Antony Chazapis
        try:
118 6d817842 Antony Chazapis
            src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
119 61efb530 Antony Chazapis
            objects = request.backend.list_objects(request.user_uniq, v_account,
120 39593b2b Giorgos Verigakis
                                src_container, prefix=src_name, virtual=False)
121 cca6c617 Antony Chazapis
        except:
122 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
123 8cb45c13 Antony Chazapis
        
124 8cb45c13 Antony Chazapis
        try:
125 8cb45c13 Antony Chazapis
            for x in objects:
126 61efb530 Antony Chazapis
                s, h = request.backend.get_object_hashmap(request.user_uniq,
127 39593b2b Giorgos Verigakis
                                        v_account, src_container, x[0], x[1])
128 8cb45c13 Antony Chazapis
                sizes.append(s)
129 8cb45c13 Antony Chazapis
                hashmaps.append(h)
130 cca6c617 Antony Chazapis
        except:
131 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
132 8cb45c13 Antony Chazapis
    else:
133 8cb45c13 Antony Chazapis
        try:
134 61efb530 Antony Chazapis
            s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
135 39593b2b Giorgos Verigakis
                                                        v_container, v_object)
136 8cb45c13 Antony Chazapis
            sizes.append(s)
137 8cb45c13 Antony Chazapis
            hashmaps.append(h)
138 cca6c617 Antony Chazapis
        except:
139 8cb45c13 Antony Chazapis
            raise ItemNotFound('Object does not exist')
140 f7b1d2af Antony Chazapis
    
141 bb4eafc6 Antony Chazapis
    if 'Content-Disposition' not in meta:
142 bb4eafc6 Antony Chazapis
        name = v_object.rstrip('/').split('/')[-1]
143 bb4eafc6 Antony Chazapis
        if not name:
144 bb4eafc6 Antony Chazapis
            name = v_public
145 bb4eafc6 Antony Chazapis
        meta['Content-Disposition'] = 'attachment; filename=%s' % (name,)
146 bb4eafc6 Antony Chazapis
    
147 8cb45c13 Antony Chazapis
    return object_data_response(request, sizes, hashmaps, meta, True)
148 f7b1d2af Antony Chazapis
149 297513ba Antony Chazapis
@api_method(user_required=False)
150 83b4c5fa Antony Chazapis
def method_not_allowed(request, **v_args):
151 f7b1d2af Antony Chazapis
    raise ItemNotFound('Object does not exist')