Editorial Workflows

七牛图床v1.0

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、上传成成功后在当前操作文档的光标处以MD语法插入图片外链地址。
3、上传成功后复制图片外链地址到系统剪贴板(可选)。
4、为保证上传速度,默认会将原图在不改变分辨率的情况下将图片文件大小缩小一倍。
注意:
1、请打开任何一个文档后再运行脚本,不然图片外链地址没地方插入。
2、请自行注册七牛免费空间。
3、使用前请先设置七牛相关参数,如不懂python请不要随便修改脚本,以免报错。
by wind.m 2017.5.20

Shared by: m.wind

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
参数设置 ?
上传脚本 ?
Source Code
#coding: utf-8
import os,time,requests,hmac,json,photos,clipboard,workflow,editor,dialogs,base64
from hashlib import sha1
from PIL import Image,ExifTags

#选择图片
img = photos.pick_image()
if not img:
	dialogs.hud_alert('未选择图片','error',0.5)
	exit()
else:
	pass
#修正图片方向
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
#生成当前时间戳作为文件名前缀
t = int(time.time())
#定义上传后图片文件名
filename = '%s%s.jpg'%(str(t),mark_str)
img = img.save(filename)
#生成七牛上传凭证
scope = cloud+':'+filename
#超时设置
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

#发送上传请求
img_file = {'file':open(filename,'rb')}
data = {'token':uptoken,'key':filename}
r = requests.post(url,data=data,files=img_file)

#结果处理
os.remove(filename)
img_url = domain+'/'+filename
insert_text = '![%s](%s)'%(mark_str.replace('_',''),img_url)
if r.status_code == 200:
#在当前文档光标处以MD语法插入图片外链
  editor.insert_text(insert_text)
#复制图片上传后外链地址到系统剪贴板
  if copy_clip == True:
  	clipboard.set(img_url)
  else:
  	pass
  dialogs.hud_alert('上传成功','success',0.5)
else:
  dialogs.hud_alert('上传失败,请检查设置参数!','error',0.5)