root / snf-pithos-app / pithos / api / public.py @ a3fcee5b
History | View | Annotate | Download (6.1 kB)
1 |
# Copyright 2011-2013 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 |
from django.http import HttpResponse |
35 |
from django.views.decorators.csrf import csrf_exempt |
36 |
|
37 |
from snf_django.lib import api |
38 |
from snf_django.lib.api import faults |
39 |
|
40 |
from pithos.api.util import (put_object_headers, update_manifest_meta, |
41 |
validate_modification_preconditions, |
42 |
validate_matching_preconditions, |
43 |
object_data_response, api_method, |
44 |
split_container_object_string) |
45 |
|
46 |
import logging |
47 |
logger = logging.getLogger(__name__) |
48 |
|
49 |
|
50 |
@csrf_exempt
|
51 |
def public_demux(request, v_public): |
52 |
if request.method == 'HEAD': |
53 |
return public_meta(request, v_public)
|
54 |
elif request.method == 'GET': |
55 |
return public_read(request, v_public)
|
56 |
else:
|
57 |
return api.method_not_allowed(request)
|
58 |
|
59 |
|
60 |
@api_method(http_method="HEAD", user_required=False, logger=logger) |
61 |
def public_meta(request, v_public): |
62 |
# Normal Response Codes: 204
|
63 |
# Error Response Codes: internalServerError (500),
|
64 |
# itemNotFound (404),
|
65 |
# badRequest (400)
|
66 |
|
67 |
try:
|
68 |
v_account, v_container, v_object = request.backend.get_public( |
69 |
request.user_uniq, |
70 |
v_public) |
71 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
72 |
v_container, v_object, 'pithos')
|
73 |
public = request.backend.get_object_public( |
74 |
request.user_uniq, v_account, |
75 |
v_container, v_object) |
76 |
except:
|
77 |
raise faults.ItemNotFound('Object does not exist') |
78 |
|
79 |
if not public: |
80 |
raise faults.ItemNotFound('Object does not exist') |
81 |
update_manifest_meta(request, v_account, meta) |
82 |
|
83 |
response = HttpResponse(status=200)
|
84 |
put_object_headers(response, meta, True)
|
85 |
return response
|
86 |
|
87 |
|
88 |
@api_method(http_method="GET", user_required=False, logger=logger) |
89 |
def public_read(request, v_public): |
90 |
# Normal Response Codes: 200, 206
|
91 |
# Error Response Codes: internalServerError (500),
|
92 |
# rangeNotSatisfiable (416),
|
93 |
# preconditionFailed (412),
|
94 |
# itemNotFound (404),
|
95 |
# badRequest (400),
|
96 |
# notModified (304)
|
97 |
|
98 |
try:
|
99 |
v_account, v_container, v_object = request.backend.get_public( |
100 |
request.user_uniq, |
101 |
v_public) |
102 |
meta = request.backend.get_object_meta(request.user_uniq, v_account, |
103 |
v_container, v_object, 'pithos')
|
104 |
public = request.backend.get_object_public( |
105 |
request.user_uniq, v_account, |
106 |
v_container, v_object) |
107 |
except:
|
108 |
raise faults.ItemNotFound('Object does not exist') |
109 |
|
110 |
if not public: |
111 |
raise faults.ItemNotFound('Object does not exist') |
112 |
update_manifest_meta(request, v_account, meta) |
113 |
|
114 |
# Evaluate conditions.
|
115 |
validate_modification_preconditions(request, meta) |
116 |
try:
|
117 |
validate_matching_preconditions(request, meta) |
118 |
except faults.NotModified:
|
119 |
response = HttpResponse(status=304)
|
120 |
response['ETag'] = meta['ETag'] |
121 |
return response
|
122 |
|
123 |
sizes = [] |
124 |
hashmaps = [] |
125 |
if 'X-Object-Manifest' in meta: |
126 |
try:
|
127 |
src_container, src_name = split_container_object_string( |
128 |
'/' + meta['X-Object-Manifest']) |
129 |
objects = request.backend.list_objects( |
130 |
request.user_uniq, v_account, |
131 |
src_container, prefix=src_name, virtual=False)
|
132 |
except:
|
133 |
raise faults.ItemNotFound('Object does not exist') |
134 |
|
135 |
try:
|
136 |
for x in objects: |
137 |
s, h = request.backend.get_object_hashmap(request.user_uniq, |
138 |
v_account, |
139 |
src_container, |
140 |
x[0], x[1]) |
141 |
sizes.append(s) |
142 |
hashmaps.append(h) |
143 |
except:
|
144 |
raise faults.ItemNotFound('Object does not exist') |
145 |
else:
|
146 |
try:
|
147 |
s, h = request.backend.get_object_hashmap( |
148 |
request.user_uniq, v_account, |
149 |
v_container, v_object) |
150 |
sizes.append(s) |
151 |
hashmaps.append(h) |
152 |
except:
|
153 |
raise faults.ItemNotFound('Object does not exist') |
154 |
|
155 |
if 'Content-Disposition' not in meta: |
156 |
name = v_object.rstrip('/').split('/')[-1] |
157 |
if not name: |
158 |
name = v_public |
159 |
meta['Content-Disposition'] = 'attachment; filename=%s' % (name,) |
160 |
|
161 |
return object_data_response(request, sizes, hashmaps, meta, True) |