forked from dongweiming/web_develop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
56 lines (41 loc) · 1.54 KB
/
client.py
File metadata and controls
56 lines (41 loc) · 1.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
# coding=utf-8
import sys
sys.path.append('gen-py')
sys.path.insert(0, '/usr/lib/python2.7/site-packages')
from thrift.transport import TTransport, TSocket
from thrift.protocol import TBinaryProtocol
from pastefile import PasteFileService
from pastefile.ttypes import (
PasteFile, CreatePasteFileRequest, UploadImageError,
NotFound)
from werkzeug.local import LocalProxy
def get_client():
transport = TSocket.TSocket('localhost', 8200)
transport = TTransport.TBufferedTransport(transport)
protocol = TBinaryProtocol.TBinaryProtocol(transport)
client = PasteFileService.Client(protocol)
transport.open()
return client
client = LocalProxy(get_client)
def create(uploaded_file, width=None, height=None):
filename = uploaded_file.filename.encode('utf-8')
mimetype = uploaded_file.mimetype.encode('utf-8')
filehash, path = client.get_file_info(filename, mimetype)
create_request = CreatePasteFileRequest()
create_request.filename = filename
create_request.mimetype = mimetype
create_request.filehash = filehash
uploaded_file.save(path)
if width is not None and height is not None:
create_request.width = width
create_request.height = height
try:
pastefile = client.create(create_request)
except UploadImageError:
return {'r': 1, 'error': 'upload fail'}
print isinstance(pastefile, PasteFile)
try:
paste_file = client.get(pastefile.id)
except NotFound:
return {'r': 1, 'error': 'not found'}
return {'r': 0, 'paste_file': paste_file}