Coverage for girder/utility/gridfs_assetstore_adapter : 96%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
#!/usr/bin/env python # -*- coding: utf-8 -*-
############################################################################### # Copyright 2013 Kitware Inc. # # Licensed under the Apache License, Version 2.0 ( the "License" ); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. ###############################################################################
# 2MB chunks. Clients must not send any chunks that are smaller than this # unless they are sending the final chunk.
""" This assetstore type stores files within mongoDB using the GridFS data model. """ def fileIndexFields():
""" :param assetstore: The assetstore to act on. """ ('uuid', pymongo.ASCENDING), ('n', pymongo.ASCENDING) ], unique=True)
""" Creates a UUID that will be used to uniquely link each chunk to """
""" Stores the uploaded chunk in fixed-sized pieces in the chunks collection of this assetstore's database. """ # Restore the internal state of the streaming SHA-512 checksum
# This bit of code will only do anything if there is a discrepancy # between the received count of the upload record and the length of # the file stored as chunks in the database. This code simply updates # the sha512 state with the difference before reading the bytes sent # from the user. cursor = self.chunkColl.find({ 'uuid': upload['chunkUuid'], 'n': {'$gte': upload['received'] // CHUNK_SIZE} }, fields=['data']).sort('n', pymongo.ASCENDING) for result in cursor: checksum.update(result['data'])
'uuid': upload['chunkUuid'] }).sort('n', pymongo.DESCENDING).limit(1) else:
'n': n, 'uuid': upload['chunkUuid'], 'data': bson.binary.Binary(data) })
# Persist the internal state of the checksum
""" The offset will be the CHUNK_SIZE * total number of chunks in the database for this file. We return the max of that and the received count because in testing mode we are uploading chunks that are smaller than the CHUNK_SIZE, which in practice will not work. """ 'uuid': upload['chunkUuid'] }).sort('n', pymongo.DESCENDING).limit(1) else:
""" Grab the final state of the checksum and set it on the file object, and write the generated UUID into the file itself. """
""" Returns a generator function that will be used to stream the file from the database to the response. """ 'attachment; filename="%s"' % file['name']
# If the file is empty, we stop here
# We must "seek" to the correct chunk index and local offset
'uuid': file['chunkUuid'], 'n': {'$gte': n} }, fields=['data']).sort('n', pymongo.ASCENDING)
else:
""" Delete all of the chunks in the collection that correspond to the given file. """ |