-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathngx_slice_cache_purge.py
31 lines (27 loc) · 1.15 KB
/
ngx_slice_cache_purge.py
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
#!/usr/bin/python3
# ngx_slice_cache_purge.py
# Purge all the slices from a cached file
# Assumes $slice = 10m (10485760 bytes)
import hashlib
import requests
import sys
from math import ceil
uri = "/uri/of/request/file/example.txt"
# real URL of file. needed to calculate total file size
url = "http://www.example.com{}".format(storage, uri)
total_size = requests.get(url,stream=True)
total_size = int(total_size.headers['Content-length'])
byte_range = 10485760
slices = ceil(total_size/byte_range)
for i in range(0,slices):.
final_part = slices-1
if(i == final_part):
byte_range_request = "bytes={}-{}".format(i*byte_range, (total_size-1))
cache_key = (uri+byte_range_request).encode("utf-8")
cache_key = hashlib.md5(cache_key).hexdigest()
print("rm -f {}/{}/{}".format(cache_key[-1], cache_key[-3]+cache_key[-2], cache_key))
else:
byte_range_request = "bytes={}-{}".format(i*byte_range, ((i+1)*byte_range)-1)
cache_key = (uri+byte_range_request).encode("utf-8")
cache_key = hashlib.md5(cache_key).hexdigest()
print("rm -f {}/{}/{}".format(cache_key[-1], cache_key[-3]+cache_key[-2], cache_key))