Skip to content

Commit

Permalink
fix bug:publish error and save config
Browse files Browse the repository at this point in the history
  • Loading branch information
CAOLINAN committed May 24, 2018
1 parent c040d24 commit bd6601b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 22 deletions.
20 changes: 10 additions & 10 deletions ulordapi/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,16 +117,16 @@ def edit(self, data):
# # "appkey": "2b111d70452f11e89c2774e6e2f53324"
# },
ulord_publish = "/transactions/publish",
ulord_publish_data = {
"author": "justin",
"title": "第一篇技术博客",
"tags": ["blockchain", "IPFS"],
"udfs_hash": "QmVcVaHhMeWNNetSLTZArmqaHMpu5ycqntx7mFZaci63VF",
"price": 0.1,
"content_type": ".txt",
"pay_password": "123",
"description": "这是使用IPFS和区块链生成的第一篇博客的描述信息"
},
# ulord_publish_data = {
# "author": "justin",
# "title": "第一篇技术博客",
# "tags": ["blockchain", "IPFS"],
# "udfs_hash": "QmVcVaHhMeWNNetSLTZArmqaHMpu5ycqntx7mFZaci63VF",
# "price": 0.1,
# "content_type": ".txt",
# "pay_password": "123",
# "description": "这是使用IPFS和区块链生成的第一篇博客的描述信息"
# },
ulord_createwallet = "/transactions/createwallet",
ulord_transaction = "/transactions/consume",
ulord_paytouser = "/transactions/paytouser",
Expand Down
12 changes: 9 additions & 3 deletions ulordapi/udfs/udfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
# @Date : 2018/5/18 0018

import sys, os, subprocess, platform, json, time, signal, logging, atexit
from uuid import uuid1

import ipfsapi

Expand Down Expand Up @@ -228,9 +229,14 @@ def upload_stream(self, stream):
# py-api doesn't support add stream.But the js-api supports.So sad.Maybe need to use HTTP-api.
start = time.time()
# TODO save stream to a file
file = "test.txt"
if fileHelper.saveFile(file, stream):
result = self.connect.add(file)
file_temp = "{}.txt".format(uuid1())
if fileHelper.saveFile(file_temp, stream):
result = self.connect.add(file_temp)
# del temp file
try:
os.remove(file_temp)
except Exception, e:
self.log.error("del temp file {0} error: {1}".format(file_temp, e))
end = time.time()
self.log.info('upload stream cost:{}'.format(end - start))
return result.get('Hash')
Expand Down
23 changes: 20 additions & 3 deletions ulordapi/up.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,16 @@ def __init__(self, appkey=None, ulord_secret=None):
self.ulord_paytouser = ulordconfig.get('ulord_url') + ulordconfig.get('ulord_paytouser') # ulord transfer webURL
# publish URL
self.ulord_publish = ulordconfig.get('ulord_url') + ulordconfig.get('ulord_publish') # ulord publish webURL
self.ulord_publish_data = ulordconfig.get('ulord_publish_data') # ulord publish data
self.ulord_publish_data = {
"author": "justin",
"title": "第一篇技术博客",
"tags": ["blockchain", "IPFS"],
"udfs_hash": "QmVcVaHhMeWNNetSLTZArmqaHMpu5ycqntx7mFZaci63VF",
"price": 0.1,
"content_type": ".txt",
"pay_password": "123",
"description": "这是使用IPFS和区块链生成的第一篇博客的描述信息"
} # ulord publish data
# query URL
self.ulord_queryblog = ulordconfig.get('ulord_url') + ulordconfig.get('ulord_queryblog') # query blog list webURL
self.ulord_checkbought = ulordconfig.get('ulord_url') + ulordconfig.get('ulord_checkbought') # query if the blog has bought
Expand Down Expand Up @@ -135,7 +144,11 @@ def post(self, url, data):
# calculate U-Sign
self.calculate_sign(data)
# self.ulord_head = ulordconfig.get('ulord_head')
r = requests.post(url=url, json=data, headers=self.ulord_head)
try:
r = requests.post(url=url, json=data, headers=self.ulord_head)
except Exception, e:
self.log.error("Failed request from the ulord-platform: {}".format(e))
return return_result(50000)
self.log.debug(r.status_code)
if r.status_code == requests.codes.ok:
self.log.debug(r.json())
Expand All @@ -153,7 +166,11 @@ def get(self, url):
"""
self.calculate_sign()
self.ulord_head = ulordconfig.get('ulord_head')
r = requests.get(url=url, headers=self.ulord_head)
try:
r = requests.get(url=url, headers=self.ulord_head)
except Exception, e:
self.log.error("Failed request from the ulord-platform: {}".format(e))
return return_result(50000)
self.log.debug(url)
self.log.debug(r.status_code)
if (r.status_code == requests.codes.ok):
Expand Down
20 changes: 15 additions & 5 deletions ulordapi/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,18 @@ def udfs_upload(self, fileinfos):
if isinstance(fileinfos, list):
for fileinfo in fileinfos:
# TODO multi threading
filehash = self.udfs.upload_file(fileinfo)
if os.path.isfile(fileinfo):
filehash = self.udfs.upload_file(fileinfo)
else:
filehash = self.udfs.upload_stream(fileinfo)
result.update({
fileinfo: filehash
})
elif isinstance(fileinfos, str):
filehash = self.udfs.upload_file(fileinfos)
else:
if os.path.isfile(fileinfos):
filehash = self.udfs.upload_file(fileinfos)
else:
filehash = self.udfs.upload_stream(fileinfos)
result.update({
fileinfos: filehash
})
Expand Down Expand Up @@ -227,7 +233,11 @@ def get_purearg(self, arg):
try:
result = self.rsahelper.decrypt(self.rsahelper.privkey, arg)
except:
self.log.info("{0} cann't decrypt,using {0}".format(arg))
# todo need to think twice
try:
self.log.info("{0} cann't decrypt,using {0}".format(arg))
except:
self.log.info("{0} cann't decrypt,using {0}".format(arg.encode('utf-8')))
if result:
return result
else:
Expand Down Expand Up @@ -411,7 +421,7 @@ def user_publish(self, title, udfshash, amount, tags, description, usercondition
username = usercondition.get('username')
current_user = User.query.filter_by(name=username).first()
elif 'usertoken' in usercondition:
token = usercondition.get('token')
token = usercondition.get('usertoken')
current_user = User.query.filter_by(token=token).first()
else:
return _errcodes.get(60100) # missing user info argument
Expand Down
4 changes: 3 additions & 1 deletion ulordapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def isUdfsHash(udfshash):
:type udfshash: str
:return: True or False
"""
if len(udfshash) == 46 and udfshash.startwith('Qm'):
if isinstance(udfshash, unicode):
udfshash = udfshash.encode('utf-8')
if len(udfshash) == 46 and udfshash.startswith('Qm'):
return True
else:
return False
Expand Down

0 comments on commit bd6601b

Please sign in to comment.