forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dropbox.dropzone
85 lines (73 loc) · 2.47 KB
/
Dropbox.dropzone
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/ruby
# Dropzone Destination Info
# Name: Dropbox
# Description: Share files via Dropbox by copying them to your Dropbox Public folder and putting the URL on the clipboard.
# Handles: NSFilenamesPboardType
# Creator: Philipp Fehre
# URL: http://sideshowcoder.com
# IconURL: http://aptonic.com/destinations/icons/dropbox.png
# Events: Dragged, Clicked
# OptionsNIB: DropboxLogin
require 'set'
require 'base64'
# Global variables for Dropbox settings
@dropboxPubDir = ""
@dropboxPublicBaseURL = "http://dl.dropbox.com/u/"
def dropbox?
# Get Dropbox Public directory from the Dropbox database file, if this file is not present
# Dropbox is most likely not installed
if File.exist?(conf_path = ENV['HOME'] + '/.dropbox/host.db')
@dropboxPubDir = Base64.decode64(File.open(conf_path).read).match(/\/.+$/)[0] + '/Public'
return true
else
return false
end
end
def dragged
$dz.determinate(true)
# Check if Dropbox is installed and set Public path
if not dropbox?
$dz.finish("Dropbox is not installed")
$dz.url(false)
return
end
# Handle Drag
if $items.length > 1
# More than 1 item dragged
# Create zip of all items and name it after the first item
dir_name = /\A(\w*)/.match($items[0].split(File::SEPARATOR).last)
zipfile = ZipFiles.zip($items, "#{dir_name}.zip")
path = zipfile
elsif File.directory?($items[0])
# 1 Folder was dragged
# Create a Zip from the folder and name it after the folder
dir_name = $items[0].split(File::SEPARATOR).last
zipfile = ZipFiles.zip($items[0], "#{dir_name}.zip")
path = zipfile
else
# Only 1 item dragged
# Handle only the file by itself
path = $items[0]
end
# Need to strip quotes which are passed when using a Zipfile
# This might be a ruby bug but the regexp should take care of that even if it is fixed
# some time in the futur
path.gsub!(/\A(['"])(.*)\1\z/, '\2')
# Copy file to Dropbox Public dir and place create URL on Clipboard
$dz.begin("Copying #{File.basename(path)} ...")
Rsync.do_copy(path, @dropboxPubDir, false)
$dz.finish("URL is now on clipboard")
$dz.url("#{@dropboxPublicBaseURL}#{ ENV['USERNAME']}/#{File.basename(path)}")
end
def clicked
# Check for Dropbox and set Public Directory path
if not dropbox?
$dz.determinate(false)
$dz.finish("Dropbox is not installed")
$dz.url(false)
else
# Open Finder at Public Directory using Applescript
`osascript -e 'tell application "Finder"' -e 'activate' -e 'open folder\
POSIX file "#{@dropboxPubDir}"' -e 'end tell'`
end
end