Merge "Update upload file utils"
This commit is contained in:
commit
62986df7d0
|
@ -13,7 +13,7 @@
|
||||||
# License for the specific language governing permissions and limitations
|
# License for the specific language governing permissions and limitations
|
||||||
# under the License.
|
# under the License.
|
||||||
|
|
||||||
"""Contains additional file utils that may be useful for upload hooks"""
|
"""Contains additional file utils that may be useful for upload hooks."""
|
||||||
|
|
||||||
import os
|
import os
|
||||||
import tempfile
|
import tempfile
|
||||||
|
@ -31,7 +31,11 @@ LOG = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
|
||||||
def create_temporary_file(stream, suffix=''):
|
def create_temporary_file(stream, suffix=''):
|
||||||
"""Create a temporary local file from a stream"""
|
"""Create a temporary local file from a stream.
|
||||||
|
|
||||||
|
:param stream: stream of bytes to be stored in a temporary file
|
||||||
|
:param suffix: (optional) file name suffix
|
||||||
|
"""
|
||||||
tfd, path = tempfile.mkstemp(suffix=suffix)
|
tfd, path = tempfile.mkstemp(suffix=suffix)
|
||||||
while True:
|
while True:
|
||||||
data = stream.read(100000)
|
data = stream.read(100000)
|
||||||
|
@ -42,8 +46,11 @@ def create_temporary_file(stream, suffix=''):
|
||||||
return tfile, path
|
return tfile, path
|
||||||
|
|
||||||
|
|
||||||
def extract_file_to_temporary_folder(tfile):
|
def extract_zip_to_temporary_folder(tfile):
|
||||||
"""Create temporary folder and extract all file contents there"""
|
"""Create temporary folder and extract all file contents there.
|
||||||
|
|
||||||
|
:param tfile: zip archive to be extracted
|
||||||
|
"""
|
||||||
zip_ref = zipfile.ZipFile(tfile, 'r')
|
zip_ref = zipfile.ZipFile(tfile, 'r')
|
||||||
tdir = tempfile.mkdtemp()
|
tdir = tempfile.mkdtemp()
|
||||||
zip_ref.extractall(tdir)
|
zip_ref.extractall(tdir)
|
||||||
|
@ -53,14 +60,22 @@ def extract_file_to_temporary_folder(tfile):
|
||||||
|
|
||||||
def upload_content_file(context, af, data, blob_dict, key_name,
|
def upload_content_file(context, af, data, blob_dict, key_name,
|
||||||
content_type='application/octet-stream'):
|
content_type='application/octet-stream'):
|
||||||
"""Upload file to blob dictionary"""
|
"""Upload a file to a blob dictionary.
|
||||||
|
|
||||||
|
:param context: user context
|
||||||
|
:param af: artifact object
|
||||||
|
:param data: bytes that need to be stored in the blob dictionary
|
||||||
|
:param blob_dict: name of the blob_dictionary field
|
||||||
|
:param key_name: name of key in the dictionary
|
||||||
|
:param content_type: (optional) specifies mime type of uploading data
|
||||||
|
"""
|
||||||
# create an an empty blob instance in db with 'saving' status
|
# create an an empty blob instance in db with 'saving' status
|
||||||
blob = {'url': None, 'size': None, 'md5': None, 'sha1': None,
|
blob = {'url': None, 'size': None, 'md5': None, 'sha1': None,
|
||||||
'sha256': None, 'status': glare_fields.BlobFieldType.SAVING,
|
'sha256': None, 'status': glare_fields.BlobFieldType.SAVING,
|
||||||
'external': False, 'content_type': content_type}
|
'external': False, 'content_type': content_type}
|
||||||
|
|
||||||
getattr(af, blob_dict)[key_name] = blob
|
getattr(af, blob_dict)[key_name] = blob
|
||||||
af = af.update_blob(context, af.id, {blob_dict: getattr(af, blob_dict)})
|
af = af.update_blob(context, af.id, blob_dict, getattr(af, blob_dict))
|
||||||
|
|
||||||
blob_id = getattr(af, blob_dict)[key_name]['id']
|
blob_id = getattr(af, blob_dict)[key_name]['id']
|
||||||
|
|
||||||
|
@ -81,11 +96,11 @@ def upload_content_file(context, af, data, blob_dict, key_name,
|
||||||
with excutils.save_and_reraise_exception(logger=LOG):
|
with excutils.save_and_reraise_exception(logger=LOG):
|
||||||
del getattr(af, blob_dict)[key_name]
|
del getattr(af, blob_dict)[key_name]
|
||||||
af = af.update_blob(context, af.id,
|
af = af.update_blob(context, af.id,
|
||||||
{blob_dict: getattr(af, blob_dict)})
|
blob_dict, getattr(af, blob_dict))
|
||||||
# update blob info and activate it
|
# update blob info and activate it
|
||||||
blob.update({'url': location_uri,
|
blob.update({'url': location_uri,
|
||||||
'status': glare_fields.BlobFieldType.ACTIVE,
|
'status': glare_fields.BlobFieldType.ACTIVE,
|
||||||
'size': size})
|
'size': size})
|
||||||
blob.update(checksums)
|
blob.update(checksums)
|
||||||
getattr(af, blob_dict)[key_name] = blob
|
getattr(af, blob_dict)[key_name] = blob
|
||||||
af.update_blob(context, af.id, {blob_dict: getattr(af, blob_dict)})
|
af.update_blob(context, af.id, blob_dict, getattr(af, blob_dict))
|
||||||
|
|
Loading…
Reference in New Issue