1 # Copyright 2011-2012 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
11 # 2. Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following
13 # disclaimer in the documentation and/or other materials
14 # provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 # CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 # POSSIBILITY OF SUCH DAMAGE.
29 # The views and conclusions contained in the software and
30 # documentation are those of the authors and should not be
31 # interpreted as representing official policies, either expressed
32 # or implied, of GRNET S.A.
36 from django.http import HttpResponse
37 from django.views.decorators.csrf import csrf_exempt
39 from pithos.api.faults import (Fault, BadRequest, ItemNotFound)
40 from pithos.api.util import (put_object_headers, update_manifest_meta,
41 validate_modification_preconditions, validate_matching_preconditions,
42 object_data_response, api_method)
43 from pithos.api.short_url import decode_url
46 logger = logging.getLogger(__name__)
50 def public_demux(request, v_public):
51 if request.method == 'HEAD':
52 return public_meta(request, v_public)
53 elif request.method == 'GET':
54 return public_read(request, v_public)
56 return method_not_allowed(request)
58 @api_method('HEAD', user_required=False)
59 def public_meta(request, v_public):
60 # Normal Response Codes: 204
61 # Error Response Codes: internalServerError (500),
66 v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
68 meta = request.backend.get_object_meta(request.user_uniq, v_account,
69 v_container, v_object, 'pithos')
70 public = request.backend.get_object_public(request.user_uniq, v_account,
71 v_container, v_object)
73 raise ItemNotFound('Object does not exist')
76 raise ItemNotFound('Object does not exist')
77 update_manifest_meta(request, v_account, meta)
79 response = HttpResponse(status=200)
80 put_object_headers(response, meta, True)
83 @api_method('GET', user_required=False)
84 def public_read(request, v_public):
85 # Normal Response Codes: 200, 206
86 # Error Response Codes: internalServerError (500),
87 # rangeNotSatisfiable (416),
88 # preconditionFailed (412),
94 v_account, v_container, v_object = request.backend.get_public(request.user_uniq,
96 meta = request.backend.get_object_meta(request.user_uniq, v_account,
97 v_container, v_object, 'pithos')
98 public = request.backend.get_object_public(request.user_uniq, v_account,
99 v_container, v_object)
101 raise ItemNotFound('Object does not exist')
104 raise ItemNotFound('Object does not exist')
105 update_manifest_meta(request, v_account, meta)
107 # Evaluate conditions.
108 validate_modification_preconditions(request, meta)
110 validate_matching_preconditions(request, meta)
112 response = HttpResponse(status=304)
113 response['ETag'] = meta['ETag']
118 if 'X-Object-Manifest' in meta:
120 src_container, src_name = split_container_object_string('/' + meta['X-Object-Manifest'])
121 objects = request.backend.list_objects(request.user_uniq, v_account,
122 src_container, prefix=src_name, virtual=False)
124 raise ItemNotFound('Object does not exist')
128 s, h = request.backend.get_object_hashmap(request.user_uniq,
129 v_account, src_container, x[0], x[1])
133 raise ItemNotFound('Object does not exist')
136 s, h = request.backend.get_object_hashmap(request.user_uniq, v_account,
137 v_container, v_object)
141 raise ItemNotFound('Object does not exist')
143 if 'Content-Disposition' not in meta:
144 name = v_object.rstrip('/').split('/')[-1]
147 meta['Content-Disposition'] = 'attachment; filename=%s' % (name,)
149 return object_data_response(request, sizes, hashmaps, meta, True)
151 @api_method(user_required=False)
152 def method_not_allowed(request, **v_args):
153 raise ItemNotFound('Object does not exist')