Repository navigation
Expand file tree
/
Copy pathdata.py
More file actions
147 lines (114 loc) · 4.54 KB
/
Copy pathdata.py
File metadata and controls
147 lines (114 loc) · 4.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
import uuid
from datetime import timedelta
import requests
from azure.storage.blob import BlobSasPermissions
from django.conf import settings
from django.utils.deconstruct import deconstructible
from django.utils.timezone import now
from utils.storage import BundleStorage
import logging
logger = logging.getLogger(__name__)
@deconstructible
class PathWrapper(object):
"""Helper to generate UUID's in file names while maintaining their extension"""
def __init__(self, base_directory, manual_override=False):
self.path = base_directory
self.manual_override = manual_override
def __call__(self, instance, filename):
if not self.manual_override:
name, extension = os.path.splitext(filename)
truncated_uuid = uuid.uuid4().hex[0:12]
truncated_name = name[0:35]
path = os.path.join(
self.path,
now().strftime('%Y-%m-%d-%s'),
truncated_uuid,
"{0}{1}".format(truncated_name, extension)
)
else:
path = os.path.join(
filename
)
return path
def make_url_sassy(path, permission='r', duration=60 * 60 * 24 * 5, content_type='application/zip'):
assert permission in ('r', 'w'), "SASSY urls only support read and write ('r' or 'w' permission)"
client_method = None # defined based on storage backend
if settings.STORAGE_IS_S3:
# Remove the beginning of the URL (before bucket name) so we just have the path to the file
path = path.split(settings.AWS_STORAGE_PRIVATE_BUCKET_NAME)[-1]
# remove prepended slash
if path.startswith('/'):
path = path[1:]
# Spaces replaced with +'s, so we have to replace those...
path = path.replace('+', ' ')
params = {
'Bucket': settings.AWS_STORAGE_PRIVATE_BUCKET_NAME,
'Key': path,
}
# AWS uses method instead of permission
if permission == 'r':
client_method = 'get_object'
elif permission == 'w':
client_method = 'put_object'
if content_type:
params["ContentType"] = content_type
return BundleStorage.bucket.meta.client.generate_presigned_url(
client_method,
Params=params,
ExpiresIn=duration,
)
elif settings.STORAGE_IS_GCS:
if permission == 'r':
client_method = 'GET'
elif permission == 'w':
client_method = 'PUT'
bucket = BundleStorage.client.get_bucket(settings.GS_PRIVATE_BUCKET_NAME)
return bucket.blob(path).generate_signed_url(
expiration=now() + timedelta(seconds=duration),
method=client_method,
content_type=content_type,
)
elif settings.STORAGE_IS_AZURE:
if permission == 'r':
client_method = BlobSasPermissions(read=True)
elif permission == 'w':
client_method = BlobSasPermissions(read=True, write=True)
sas_token = BundleStorage.service.generate_blob_shared_access_signature(
BundleStorage.azure_container,
path,
client_method,
expiry=now() + timedelta(seconds=duration),
)
return BundleStorage.service.make_blob_url(
container_name=BundleStorage.azure_container,
blob_name=path,
sas_token=sas_token,
)
def put_blob(url, file_path):
return requests.put(
url,
data=open(file_path, 'rb'),
headers={
# Only for Azure but AWS ignores this fine
'x-ms-blob-type': 'BlockBlob',
}
)
def pretty_bytes(bytes, decimal_places=1, suffix="B", binary=False, return_0_for_invalid=False):
# Ensure bytes is a valid number
try:
bytes = float(bytes)
except (ValueError, TypeError):
return 0 if return_0_for_invalid else "" # Return 0 or empty string for invalid inputs
if bytes < 0:
return 0 if return_0_for_invalid else "" # Return 0 or empty string for invalid inputs
factor = 1024.0 if binary else 1000.0
units = ['', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi'] if binary else ['', 'k', 'M', 'G', 'T', 'P', 'E', 'Z']
for unit in units:
if abs(bytes) < factor or unit == (units[-1] + "B"):
return f"{bytes:.{decimal_places}f} {unit}{suffix}"
bytes /= factor
return f"{bytes:.{decimal_places}f}{units[-1]}{suffix}"
def gb_to_bytes(gb, binary=False):
factor = 1024**3 if binary else 1000**3
return gb * factor