forked from a4agarwal/dropzone-user-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAFP Copy.dropzone
executable file
·211 lines (189 loc) · 7.07 KB
/
AFP Copy.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#!/usr/bin/ruby
# Dropzone Destination Info
# Name: AFP Copy
# Description: Copy files to a specified path on a specified server.
# Handles: NSFilenamesPboardType
# Creator: Aloha Silver
# URL: http://alohasilver.wordpress.com/afp-copy/
# IconURL: http://alohasilver.files.wordpress.com/2010/03/downloads.png
# Events: Dragged, Clicked, TestConnection
# OptionsNIB: ExtendedLogin
def dragged
# Verifies if the server is online
if isonline?
itisonline = true
else
itisonline = false
end
# In case the server is offline, tells the user that and stops acting
if itisonline == false
$dz.finish("This Server is Offline!")
$dz.url(false)
return
# In case the server is online, acts as expected, copying the files
else
# Checks for a mounted volume.
path = "/Volumes/#{ENV['USERNAME']}/"
if File.exists?(path) && File.directory?(path)
tomount = false
else
tomount = true
end
# Mounts the volume, if it wasn't mounted before running the script.
if tomount == true
case failproof
when 0
$dz.determinate(true)
$dz.begin("Mounting Volume...")
`mkdir /Volumes/#{ENV['USERNAME']}`
`/sbin/mount_afp afp://#{ENV['USERNAME']}:#{ENV['PASSWORD']}@"#{ENV['SERVER']}"/#{ENV['USERNAME']} /Volumes/#{ENV['USERNAME']}`
when 1
$dz.error("Authentication Error", "You provided either a wrong username or a wrong password.\nPlease check your settings and test the connection.")
when 2
$dz.error("User Folder Missing", "You provided a wrong username.\nPlease check your settings and test the connection.")
when 3
$dz.error("Wrong Path", "You provided an invalid path on the remote server.\nPlease check your settings and test the connection.")
when 5
$dz.error("Unknown Error", "An Unknown error has ocurred.")
end
end
# Copies the dragged file(s)
$dz.begin("Copying...")
Rsync.do_copy($items, "/Volumes/#{ENV['USERNAME']}/#{ENV['REMOTE_PATH']}", false)
# If the script mounted the volume, it is then ejected.
if tomount == true
`umount /Volumes/#{ENV['USERNAME']}`
end
$dz.finish("File(s) Copied")
$dz.url(false)
end
end
def clicked
# Verifies if the server is online
if isonline?
itisonline = true
else
itisonline = false
end
# In case the server is offline, tells the user that and stops acting
if itisonline == false
$dz.finish("This Server is Offline!")
$dz.url(false)
return
# In case the server is online, acts as expected, opening the folder
else
# Checks for a mounted volume.
path = "/Volumes/#{ENV['USERNAME']}/"
if File.exists?(path) && File.directory?(path)
tomount = false
else
tomount = true
end
# Mounts the volume, if it wasn't mounted before running the script.
# It also double checks for errors.
if tomount == true
case failproof
when 0
$dz.determinate(false)
$dz.begin("Mounting Volume...")
`mkdir /Volumes/#{ENV['USERNAME']}`
`/sbin/mount_afp afp://#{ENV['USERNAME']}:#{ENV['PASSWORD']}@"#{ENV['SERVER']}"/#{ENV['USERNAME']} /Volumes/#{ENV['USERNAME']}`
system("open \"#{"/Volumes/#{ENV['USERNAME']}/#{ENV['REMOTE_PATH']}"}\"")
$dz.finish("Done")
$dz.url(false)
when 1
$dz.error("Authentication Error", "You provided either a wrong username or a wrong password.\nPlease check your settings and test the connection.")
when 2
$dz.error("User Folder Missing", "You provided a wrong username.\nPlease check your settings and test the connection.")
when 3
$dz.error("Wrong Path", "You provided an invalid path on the remote server.\nPlease check your settings and test the connection.")
when 5
$dz.error("Unknown Error", "An Unknown error has ocurred.")
end
# If the volume was already mounted, just shows the folder
else
system("open \"#{"/Volumes/#{ENV['USERNAME']}/#{ENV['REMOTE_PATH']}"}\"")
$dz.finish("Done")
$dz.url(false)
end
end
end
def isonline?
# This function tests for the availability of a server.
# It returns true if the server is online, false if it is offline.
pinganswer = `ping -t 1 -o #{ENV['SERVER']}`
if pinganswer == ''
return false
else
return true
end
end
def testconnection
# This function allows the user to test the connection before
# adding the script to the grid
# Verifies if the server is online
if isonline?
itisonline = true
else
itisonline = false
end
# In case the server is offline, tells the user that and stops acting
if itisonline == false
$dz.error("Could Not Reach Server.", "There is a problem with this server.\nIt is either offline or nonexistent.")
end
# In case the server is online, verifies if it is already mounted
path = "/Volumes/#{ENV['USERNAME']}/"
if File.exists?(path) && File.directory?(path)
$dz.error("Server Mounted", "Please eject the server and try again.\nThis is only needed for this test.")
# In case the server is online and not mounted, acts as expected, checking the info provided
else
`mkdir /Volumes/#{ENV['USERNAME']}`
`/sbin/mount_afp afp://#{ENV['USERNAME']}:#{ENV['PASSWORD']}@"#{ENV['SERVER']}"/#{ENV['USERNAME']}/#{ENV['REMOTE_PATH']} /Volumes/#{ENV['USERNAME']}`
mount_success = $?
case mount_success
when 0
`umount /Volumes/#{ENV['USERNAME']}`
$dz.alert("Connection Successful", "The script is properly configured")
#$dz.alert("Connection Successful", mount_success)
when 24832
system("rmdir /Volumes/#{ENV['USERNAME']}")
$dz.error("Authentication Error", "There is a problem with either your username or your password.\nCould not authenticate.")
#$dz.error("Authentication Error", mount_success)
when 30720
system("rmdir /Volumes/#{ENV['USERNAME']}")
$dz.error("User Folder Missing", "This user folder does not exist on the remote server.")
#$dz.error("No Folder", mount_success)
when 25856
system("rmdir /Volumes/#{ENV['USERNAME']}")
$dz.error("Wrong Path", "This path does not exist on the remote server.\nPlease check the remote path field.")
#when 14848
#$dz.error("Server Mounted", "Please eject the server and try again.\nThis is only needed for this test.")
else
$dz.error("Unknown Error", "An Unknown error has ocurred.")
#$dz.error("Unknown Error", mount_success)
end
end
end
def failproof
`mkdir /Volumes/#{ENV['USERNAME']}`
`/sbin/mount_afp afp://#{ENV['USERNAME']}:#{ENV['PASSWORD']}@"#{ENV['SERVER']}"/#{ENV['USERNAME']}/#{ENV['REMOTE_PATH']} /Volumes/#{ENV['USERNAME']}`
mount_success = $?
case mount_success
when 0
`umount /Volumes/#{ENV['USERNAME']}`
return 0
when 24832
system("rmdir /Volumes/#{ENV['USERNAME']}")
return 1
when 30720
system("rmdir /Volumes/#{ENV['USERNAME']}")
return 2
when 25856
system("rmdir /Volumes/#{ENV['USERNAME']}")
return 3
#when 14848
#$dz.error("Server Mounted", "Please eject the server and try again.\nThis is only needed for this test.")
else
return 5
end
end