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、上传成成功后在当前操作文档的光标处以MD语法插入图片外链地址。
3、上传成功后复制图片外链地址到系统剪贴板(可选)。
注意:
1、请打开任何一个文档后再运行脚本,不然图片外链地址没地方插入。
2、请自行注册七牛免费空间。
3、使用前请先设置七牛相关参数,如不懂python请不要随便修改脚本,以免报错。
by wind.m 2017.6.5 Tg:@meiycs

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,io
from hashlib import sha1
from PIL import Image,ExifTags

#选择图片
if select_mode == '0':
	img_pick = photos.pick_asset(assets=photos.get_assets(),title='请选择需要插入的图片')
elif select_mode == '1':
	img_pick = photos.get_assets()[-1]
else:
	try:
		img_pick = photos.get_screenshots_album().assets[-1]
	except:
		dialogs.hud_alert('相册内未发现屏幕截图片','error',1)
		exit()

if not img_pick:
	dialogs.hud_alert('未选择图片','error',1)
	exit()
else:
	pass

img = img_pick.get_image()


if img.format == 'GIF':
	img_ext = 'GIF'
	img_name_ext = 'gif'
	img_data = img_pick.get_image_data()
else:
	img_ext = 'JPEG'
	img_name_ext = 'jpg'
	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

	x = (int(img.size[0]*resize_set),int(img.size[1]*resize_set))
	img = img.resize(x)
	img_data = io.BytesIO()
	img.save(img_data,format=img_ext,quality=quality_set)

#生成当前时间戳作为文件名前缀
t = int(time.time())

#定义上传后图片文件名
filename = '%s%s.%s'%(str(t),mark_str,img_name_ext)

#生成七牛上传凭证
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':img_data.getvalue()}
data = {'token':uptoken,'key':filename}
r = requests.post(url,data=data,files=img_file)
#结果处理
img_data.close()
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)