root / snf-pithos-app / pithos / api / public.py @ 56f3c759
History | View | Annotate | Download (6.2 kB)
1 |
# Copyright 2011-2012 GRNET S.A. All rights reserved.
|
---|---|
2 |
#
|
3 |
# Redistribution and use in source and binary forms, with or
|
4 |
# without modification, are permitted provided that the following
|
5 |
# conditions are met:
|
6 |
#
|
7 |
# 1. Redistributions of source code must retain the above
|
8 |
# copyright notice, this list of conditions and the following
|
9 |
# disclaimer.
|
10 |
#
|
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.
|
15 |
#
|
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.
|
28 |
#
|
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.
|
33 |
|
34 |
import logging |
35 |
|
36 |
from django.http import HttpResponse |
37 |
from django.views.decorators.csrf import csrf_exempt |
38 |
|
39 |
from synnefo.lib.astakos import get_user |
40 |
|
41 |
from pithos.api.faults import (Fault, BadRequest, ItemNotFound, NotModified) |
42 |
from pithos.api.util import (put_object_headers, update_manifest_meta, |
43 |
validate_modification_preconditions, |
44 |
validate_matching_preconditions, |
45 |
object_data_response, api_method, |
46 |
split_container_object_string) |
47 |
from pithos.api.settings import AUTHENTICATION_URL, AUTHENTICATION_USERS |
48 |
|
49 |
|
50 |
logger = logging.getLogger(__name__) |
51 |
|
52 |
|
53 |
@csrf_exempt
|
54 |
def public_demux(request, v_public): |
55 |
get_user(request, AUTHENTICATION_URL, AUTHENTICATION_USERS) |
56 |
if request.method == 'HEAD': |
57 |
return public_meta(request, v_public)
|
58 |
elif request.method == 'GET': |
59 |
return public_read(request, v_public)
|
60 |
else:
|
61 |
return method_not_allowed(request)
|
62 |
|
63 |
|
64 |
@api_method('HEAD', user_required=False) |
65 |
def public_meta(request, v_public): |
66 |
# Normal Response Codes: 204
|
67 |
# Error Response Codes: internalServerError (500),
|
68 |
# itemNotFound (404),
|
69 |
# badRequest (400)
|
70 |
|
71 |
try:
|
72 |
v_account, v_container, v_object = request.backend.get_public( |
73 |
request.user_uniq, |
74 |
v_public) |
75 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
76 |
v_container, v_object, 'pithos')
|
77 |
public = request.backend.get_object_public( |
78 |
request.user_uniq, v_account, |
79 |
v_container, v_object) |
80 |
except:
|
81 |
raise ItemNotFound('Object does not exist') |
82 |
|
83 |
if not public: |
84 |
raise ItemNotFound('Object does not exist') |
85 |
update_manifest_meta(request, v_account, meta) |
86 |
|
87 |
response = HttpResponse(status=200)
|
88 |
put_object_headers(response, meta, True)
|
89 |
return response
|
90 |
|
91 |
|
92 |
@api_method('GET', user_required=False) |
93 |
def public_read(request, v_public): |
94 |
# Normal Response Codes: 200, 206
|
95 |
# Error Response Codes: internalServerError (500),
|
96 |
# rangeNotSatisfiable (416),
|
97 |
# preconditionFailed (412),
|
98 |
# itemNotFound (404),
|
99 |
# badRequest (400),
|
100 |
# notModified (304)
|
101 |
|
102 |
try:
|
103 |
v_account, v_container, v_object = request.backend.get_public( |
104 |
request.user_uniq, |
105 |
v_public) |
106 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
107 |
v_container, v_object, 'pithos')
|
108 |
public = request.backend.get_object_public( |
109 |
request.user_uniq, v_account, |
110 |
v_container, v_object) |
111 |
except:
|
112 |
raise ItemNotFound('Object does not exist') |
113 |
|
114 |
if not public: |
115 |
raise ItemNotFound('Object does not exist') |
116 |
update_manifest_meta(request, v_account, meta) |
117 |
|
118 |
# Evaluate conditions.
|
119 |
validate_modification_preconditions(request, meta) |
120 |
try:
|
121 |
validate_matching_preconditions(request, meta) |
122 |
except NotModified:
|
123 |
response = HttpResponse(status=304)
|
124 |
response['ETag'] = meta['ETag'] |
125 |
return response
|
126 |
|
127 |
sizes = [] |
128 |
hashmaps = [] |
129 |
if 'X-Object-Manifest' in meta: |
130 |
try:
|
131 |
src_container, src_name = split_container_object_string( |
132 |
'/' + meta['X-Object-Manifest']) |
133 |
objects = request.backend.list_objects( |
134 |
request.user_uniq, v_account, |
135 |
src_container, prefix=src_name, virtual=False)
|
136 |
except:
|
137 |
raise ItemNotFound('Object does not exist') |
138 |
|
139 |
try:
|
140 |
for x in objects: |
141 |
s, h = request.backend.get_object_hashmap(request.user_uniq, |
142 |
v_account, src_container, x[0], x[1]) |
143 |
sizes.append(s) |
144 |
hashmaps.append(h) |
145 |
except:
|
146 |
raise ItemNotFound('Object does not exist') |
147 |
else:
|
148 |
try:
|
149 |
s, h = request.backend.get_object_hashmap( |
150 |
request.user_uniq, v_account, |
151 |
v_container, v_object) |
152 |
sizes.append(s) |
153 |
hashmaps.append(h) |
154 |
except:
|
155 |
raise ItemNotFound('Object does not exist') |
156 |
|
157 |
if 'Content-Disposition' not in meta: |
158 |
name = v_object.rstrip('/').split('/')[-1] |
159 |
if not name: |
160 |
name = v_public |
161 |
meta['Content-Disposition'] = 'attachment; filename=%s' % (name,) |
162 |
|
163 |
return object_data_response(request, sizes, hashmaps, meta, True) |
164 |
|
165 |
|
166 |
@api_method(user_required=False) |
167 |
def method_not_allowed(request, **v_args): |
168 |
raise ItemNotFound('Object does not exist') |