Revision 133e3fcf snf-pithos-app/pithos/api/util.py

b/snf-pithos-app/pithos/api/util.py
62 62
                                 BACKEND_BLOCK_SIZE, BACKEND_HASH_ALGORITHM,
63 63
                                 RADOS_STORAGE, RADOS_POOL_BLOCKS,
64 64
                                 RADOS_POOL_MAPS, TRANSLATE_UUIDS,
65
                                 PUBLIC_URL_SECURITY,
66
                                 PUBLIC_URL_ALPHABET,
67
                                 COOKIE_NAME, BASE_HOST)
65
                                 PUBLIC_URL_SECURITY, PUBLIC_URL_ALPHABET,
66
                                 COOKIE_NAME, BASE_HOST, UPDATE_MD5)
68 67
from pithos.api.resources import resources
69 68
from pithos.backends.base import (NotAllowedError, QuotaError, ItemNotExists,
70 69
                                  VersionNotExists)
......
216 215

  
217 216

  
218 217
def put_object_headers(response, meta, restricted=False, token=None):
219
    response['ETag'] = meta['checksum']
218
    response['ETag'] = meta['hash'] if not UPDATE_MD5 else meta['checksum']
220 219
    response['Content-Length'] = meta['bytes']
221 220
    response.override_serialization = True
222 221
    response['Content-Type'] = meta.get('type', 'application/octet-stream')
......
260 259
                request.user_uniq, v_account,
261 260
                src_container, prefix=src_name, virtual=False)
262 261
            for x in objects:
263
                src_meta = request.backend.get_object_meta(request.user_uniq,
264
                                                           v_account,
265
                                                           src_container,
266
                                                           x[0], 'pithos', x[1])
267
                etag += src_meta['checksum']
262
                src_meta = request.backend.get_object_meta(
263
                    request.user_uniq, v_account, src_container, x[0],
264
                    'pithos', x[1])
265
                etag += (src_meta['hash'] if not UPDATE_MD5 else
266
                         src_meta['checksum'])
268 267
                bytes += src_meta['bytes']
269 268
        except:
270 269
            # Ignore errors.
......
424 423
def validate_matching_preconditions(request, meta):
425 424
    """Check that the ETag conforms with the preconditions set."""
426 425

  
427
    etag = meta['checksum']
426
    etag = meta['hash'] if not UPDATE_MD5 else meta['checksum']
428 427
    if not etag:
429 428
        etag = None
430 429

  
......
773 772
        if len(self.data) >= length:
774 773
            block = self.data[:length]
775 774
            self.file.hashmap.append(self.backend.put_block(block))
776
            self.md5.update(block)
775
            self.checksum_compute.update(block)
777 776
            self.data = self.data[length:]
778 777

  
779 778
    def new_file(self, field_name, file_name, content_type,
780 779
                 content_length, charset=None):
781
        self.md5 = hashlib.md5()
780
        self.checksum_compute = NoChecksum() if not UPDATE_MD5 else Checksum()
782 781
        self.data = ''
783 782
        self.file = UploadedFile(
784 783
            name=file_name, content_type=content_type, charset=charset)
......
795 794
        l = len(self.data)
796 795
        if l > 0:
797 796
            self.put_data(l)
798
        self.file.etag = self.md5.hexdigest().lower()
797
        self.file.etag = self.checksum_compute.hexdigest()
799 798
        return self.file
800 799

  
801 800

  
......
1161 1160
                raise Exception(response)
1162 1161
        return wrapper
1163 1162
    return decorator
1163

  
1164

  
1165
class Checksum:
1166
    def __init__(self):
1167
        self.md5 = hashlib.md5()
1168

  
1169
    def update(self, data):
1170
        self.md5.update(data)
1171

  
1172
    def hexdigest(self):
1173
        return self.md5.hexdigest().lower()
1174

  
1175
class NoChecksum:
1176
    def update(self, data):
1177
        pass
1178

  
1179
    def hexdigest(self):
1180
        return ''

Also available in: Unified diff