forked from qiniu/qetag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qetag.py
executable file
·79 lines (58 loc) · 1.66 KB
/
qetag.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
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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# 最新版本 https://github.com/qiniu/python-sdk/blob/master/qiniu/utils.py#L129
import os
import sys
import base64
import hashlib
try:
from cStringIO import StringIO as BytesIO # py2
bytes_chr = chr
except ImportError:
from io import BytesIO # py3
bytes_chr = lambda c: bytes([c])
CHUNK_BITS = 22
CHUNK_SIZE = 1 << CHUNK_BITS # == 2 ** 22 == 4 * 1024 * 1024 == 4MiB
def ensure_bytes(text, encoding='U8'):
return text if isinstance(text, bytes) else text.encode(encoding)
def get_io_size(fio):
"""get file size from fio"""
fio.seek(0, os.SEEK_END)
fsize = fio.tell()
fio.seek(0)
return fsize
def get_io_qetag(fio):
"""Caculates qetag from file object
Parameters:
- fio: file-like object to the file
Usage:
>>> data = bytes_chr(0) * (CHUNK_SIZE + 42) * 42
>>> fio = BytesIO(data)
>>> print(get_io_qetag(fio))
lnmoz9lrkr6HWgZyTqu2vD0XUj6R
Returns qetag
"""
size = get_io_size(fio)
flag = CHUNK_BITS
sha1 = hashlib.sha1
buf = []
while size > 0:
size -= CHUNK_SIZE
buf.append(sha1(fio.read(CHUNK_SIZE)).digest())
buf = b''.join(buf)
if len(buf) > 20: # more than 1 chunk
flag |= 0x80
buf = sha1(buf).digest()
fio.seek(0)
return base64.urlsafe_b64encode(bytes_chr(flag) + buf).decode('ASCII')
def get_qetag(filename):
"""Caculates qetag
Parameters:
- filename: string, file name
Returns qetag
"""
with open(filename, 'rb') as fp:
return get_io_qetag(fp)
if __name__ == '__main__':
assert len(sys.argv) > 1
print(get_qetag(sys.argv[1]))