Editorial Workflows

SFTP Image Upload

unlisted 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: Uploads an image to a host using SFTP. The image can be uploaded from the clipboard or selected from the camera roll.

There is also an option to resize the image proportionally based on a width specified by the user.

After upload the URL to the image is placed on the clipboard.

Image files are suffixed with a time stamp before upload.

::: You must enter your own host credentials, upload path and root URL for the hosted image.

::: Made by Macdrifter

Comments: Comment Feed (RSS)

There are no comments yet.

+ Add Comment

Workflow Preview
Show Alert ?
Title
Source
Message
Locate Image
Button 1
Clipboard
Output Value
clipboard
Button 2
Photo Roll
Output Value
photo
Button 3
(don't show)
Output Value
Show Cancel Button
ON
Set Variable ?
Variable Name
source
Value
Input
Select from List ?
Title
Image Width
List (Lines)
600 400 250 Custom Cancel
Multiple Selection
OFF
Show in Popover
OFF
Set Variable ?
Variable Name
widthSelection
Value
Input
If… ?
Run the block if
widthSelection
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Custom
Request Text Input ?
Title
Custom Size
Initial Text
  • Single Line
  • Multiple Lines
Keyboard Options:
…End If
If… ?
Run the block if
widthSelection
  • is Equal to
  • is Not Equal to
  • Contains
  • Doesn't Contain
  • Matches Regular Expression
Cancel
Stop ?
Stop
  • This Workflow
  • Repeat Block
Show HUD Alert
OFF
Message
Stopped
…End If
Run Python Script ?
Source Code
#coding: utf-8
import workflow
import Image, ImageOps, ImageFilter
import console
import clipboard
import datetime
from io import BytesIO
import urllib
import editor
import keychain
import pickle
import paramiko
import photos

#keychain.delete_password('macdrifter', 'editorial')
login = keychain.get_password('macdrifter_ssh', 'editorial')
if login is not None:
	user, pw = pickle.loads(login)
else:
	user, pw = console.login_alert('FTPS Login Needed', 'No login credentials found.')
	pickle_token = pickle.dumps((user, pw))
	keychain.set_password('macdrifter_ssh', 'editorial', pickle_token)


width_selection = workflow.get_variable('widthSelection')
source_selection = workflow.get_variable('source')


host = "myhost.com"
port = 22
url_base = "http://myhost.com/uploads/"
remote_path = "/actual/remote/file/path/to/upload/"

if source_selection == 'photo':
	image_selection = photos.pick_image()
else:
	image_selection = clipboard.get_image()
	if not image_selection:
		console.alert('No Image', 'Clipboard does not contain an image')



today = datetime.datetime.now()

file_name = console.input_alert("Image Title", "Enter Image File Name")
file_name = file_name+'_'+today.strftime("%Y-%m-%d-%H%M%S") +'.png'


date_path = today.strftime("%Y/%m/")
# Used to create full remote file path
remote_date_path =  remote_path + date_path


def customSize(img, new_width):
	w, h = img.size
	if w > new_width:
		wsize = float(new_width)/float(w)
		hsize = int(float(h)*float(wsize))
		img = img.resize((new_width, hsize), Image.ANTIALIAS)
		print str(new_width)+'w X '+str(hsize)+'h'
	else: 
		print 'Image not resized. Width less than '+new_width
	return img

image = customSize(image_selection, int(width_selection))
image.show()


buffer = BytesIO()
image.save(buffer, 'PNG')
buffer.seek(0)


file_url = urllib.quote(file_name)


try:

	transport = paramiko.Transport((host, port))
	transport.connect(username=user, password=pw)
	sftp = paramiko.SFTPClient.from_transport(transport)
	#sftp.chdir(remote_path)
	f = sftp.open(remote_date_path+file_name, 'wb')
	f.write(buffer.read())
	f.close()

	sftp.close()
	transport.close()
	console.hud_alert(file_name + ' uploaded', 'success')

except Exception, e:
	print e
	console.alert('Error', e)

image_link = url_base+date_path+file_url
print(image_link)
clipboard.set(image_link)
console.hud_alert('Remote URL on Clipboard ', 'success')