Editorial Workflows

七牛图床

public workflow

Install Workflow...

This workflow contains at least one Python script. Only use it if you trust the person who shared this with you, and if you know exactly what it does.

I understand, install the workflow!

This is a workflow for Editorial, a Markdown and plain text editor for iOS. To download it, you need to view this page on a device that has the app installed.

Description: 1、利用七牛免费空间作为图床,选择相册图片一键上传并插入地址到当前文档光标位置。
2、使用前先先在自定义参数脚本内按说明填入你的七牛域名、空间名、AK密匙、SK密匙。
3、请在打开任何一个文档的情况下运行。

Shared by: wind

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
输入参数 ?
Source Code
#coding: utf-8
import workflow

#请在下面引号间输入初始参数
#输入你的七牛AK
ak='这里填入你的ak'
#输入你的七牛SK
sk='这里填入你的sk'
#输入你的空间名
cloud='这里填入你的空间名'
#输入你的七牛域名
domain='这里填入你的七牛域名,以http开头,以.com结尾'
图片预处理 ?
Source Code
#coding: utf-8
from PIL import Image, ExifTags
import photos
import workflow
import console
import editor
import os
import io
import time

#生成时间戳作为文件名
t=time.time()
timestr=int(t)
#选择图片并压缩
img=photos.pick_image()
if not img:
	workflow.stop()
try:
	for orientation, value in ExifTags.TAGS.items():
		if value=='Orientation':
			break
	exif=dict(img._getexif().items())
	rot_degrees={3: 180, 6: 270, 8: 90}.get(exif[orientation], 0)
	if rot_degrees:
		img=img.rotate(rot_degrees, expand=True)
except (AttributeError, KeyError, IndexError):
	pass
#获取当前文档绝对路径	
doc_path=editor.get_path()
doc_dir, fn=os.path.split(doc_path)
default_name='{}_image'.format(timestr)
i = 1
while True:
	if not os.path.exists(os.path.join(doc_dir, default_name + '.jpg')):
		break
	default_name='{}_image_{}'.format(timestr, i)
	i += 1
root, rel_doc_path=editor.to_relative_path(editor.get_path())
filename=default_name + '.jpg'
if not filename:
	workflow.stop()
#临时保存到当前文档路径供post请求调用
img_data=io.BytesIO()
img.save(img_data, 'jpeg')
rel_doc_dir, fn=os.path.split(rel_doc_path)
dest_path=os.path.join(rel_doc_dir, filename)
editor.set_file_contents(dest_path, img_data.getvalue(), root)
上传图片 ?
Source Code
#coding: utf-8
import time
import requests
import os
import console
import hmac
import hashlib
from hashlib import sha1
import requests.auth
import urlparse
import base64
from base64 import urlsafe_b64encode
import json

#生成七牛上传凭证
scope=cloud+':'+filename
#超时设置
t=time.time()
deadline=int(t)+3600
#编码凭证
data={'scope':scope,'deadline':deadline}
json=json.dumps(data)
encode_json=base64.urlsafe_b64encode(json)
sign=hmac.new(sk,encode_json,sha1).digest()
encode_sign=base64.urlsafe_b64encode(sign)
uptoken=ak+':'+encode_sign+':'+encode_json

#发送上传请求
url='http://up-z2.qiniu.com'
path=doc_dir+'/'+filename
files={'file':open(path,'rb')}
data={'token':uptoken,'key':filename}
r=requests.post(url,data=data,files=files)
status=r.status_code

#结果处理
os.remove(path)
img_url=domain+'/'+filename
insert_text='!'+'[]'+'('+img_url+')'
if status==200:
  console.hud_alert('上传成功')
  editor.insert_text(insert_text)
else:
  console.hud_alert('上传失败,请重试!')