1 | # ---------------------------------------------------------------------- |
---|
2 | # COMPONENT: filexfer - support for file transfer with user's desktop |
---|
3 | # |
---|
4 | # Supports interactions with the filexfer middleware, enabling |
---|
5 | # Rappture to transfer files to and from the user's desktop. |
---|
6 | # Files are downloaded by invoking "exportfile", and uploaded by |
---|
7 | # invoking "importfile". The middleware handles the rest. |
---|
8 | # ====================================================================== |
---|
9 | # AUTHOR: Michael McLennan, Purdue University |
---|
10 | # Copyright (c) 2004-2007 Purdue Research Foundation |
---|
11 | # |
---|
12 | # See the file "license.terms" for information on usage and |
---|
13 | # redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
---|
14 | # ====================================================================== |
---|
15 | package require BLT |
---|
16 | |
---|
17 | namespace eval Rappture { # forward declaration } |
---|
18 | namespace eval Rappture::filexfer { |
---|
19 | variable enabled 0 ;# set to 1 when middleware is in place |
---|
20 | variable commands ;# complete path to exportfile/importfile |
---|
21 | variable job ;# used to manage bgexec jobs |
---|
22 | } |
---|
23 | |
---|
24 | # ---------------------------------------------------------------------- |
---|
25 | # USAGE: Rappture::filexfer::init |
---|
26 | # |
---|
27 | # Called in the main application to see if the filexfer middleware |
---|
28 | # is installed. Returns 1 if the middleware is installed, and 0 |
---|
29 | # otherwise. |
---|
30 | # ---------------------------------------------------------------------- |
---|
31 | proc Rappture::filexfer::init {} { |
---|
32 | variable enabled |
---|
33 | variable commands |
---|
34 | |
---|
35 | # |
---|
36 | # Look for the exportfile/importfile commands and see if |
---|
37 | # they appear to be working. If we have both, then this |
---|
38 | # is "enabled". |
---|
39 | # |
---|
40 | foreach op {export import} { |
---|
41 | set prog "${op}file" |
---|
42 | set path [auto_execok $prog] |
---|
43 | if {"" == $path} { |
---|
44 | foreach dir {/apps/bin /apps/filexfer/bin} { |
---|
45 | set p [file join $dir $prog] |
---|
46 | if {[file executable $p]} { |
---|
47 | set path $p |
---|
48 | break |
---|
49 | } |
---|
50 | } |
---|
51 | } |
---|
52 | if {[file executable $path]} { |
---|
53 | # the new filexfer has a --provenance arg for file names |
---|
54 | set info [exec $path --help 2>@1] |
---|
55 | if {[regexp -- {--provenance} $info]} { |
---|
56 | lappend path --provenance |
---|
57 | } |
---|
58 | set commands($op) $path |
---|
59 | } else { |
---|
60 | return 0 |
---|
61 | } |
---|
62 | } |
---|
63 | |
---|
64 | return [set enabled 1] |
---|
65 | } |
---|
66 | |
---|
67 | # ---------------------------------------------------------------------- |
---|
68 | # USAGE: Rappture::filexfer::enabled |
---|
69 | # |
---|
70 | # Clients use this to see if the filexfer stuff is up and running. |
---|
71 | # If so, then the GUI will provide "Download..." and other filexfer |
---|
72 | # options. If not, then Rappture must be running within an |
---|
73 | # environment that doesn't support it. As a backup, Rappture provides |
---|
74 | # file load/save operations instead. |
---|
75 | # ---------------------------------------------------------------------- |
---|
76 | proc Rappture::filexfer::enabled {} { |
---|
77 | variable enabled |
---|
78 | return $enabled |
---|
79 | } |
---|
80 | |
---|
81 | # ---------------------------------------------------------------------- |
---|
82 | # USAGE: Rappture::filexfer::label upload|download |
---|
83 | # |
---|
84 | # Clients use this to query the proper label for an "upload" or |
---|
85 | # "download" opertion. These are usually "Upload..." and "Download", |
---|
86 | # but could change to "Load File..." and "Save As..." for applications |
---|
87 | # in desktop mode. |
---|
88 | # ---------------------------------------------------------------------- |
---|
89 | proc Rappture::filexfer::label {operation} { |
---|
90 | switch -- $operation { |
---|
91 | upload { |
---|
92 | if {[Rappture::filexfer::enabled]} { |
---|
93 | return "Upload..." |
---|
94 | } else { |
---|
95 | return "Load File..." |
---|
96 | } |
---|
97 | } |
---|
98 | uploadWord { |
---|
99 | if {[Rappture::filexfer::enabled]} { |
---|
100 | return "Upload" |
---|
101 | } else { |
---|
102 | return "Browse" |
---|
103 | } |
---|
104 | } |
---|
105 | download { |
---|
106 | if {[Rappture::filexfer::enabled]} { |
---|
107 | return "Download" |
---|
108 | } else { |
---|
109 | return "Save As..." |
---|
110 | } |
---|
111 | } |
---|
112 | downloadWord { |
---|
113 | if {[Rappture::filexfer::enabled]} { |
---|
114 | return "Download" |
---|
115 | } else { |
---|
116 | return "Save" |
---|
117 | } |
---|
118 | } |
---|
119 | default { |
---|
120 | error "bad option \"$operation\": should be upload, uploadWord, download, or downloadWord" |
---|
121 | } |
---|
122 | } |
---|
123 | } |
---|
124 | |
---|
125 | |
---|
126 | # ---------------------------------------------------------------------- |
---|
127 | # USAGE: Rappture::filexfer::upload <toolName> <controlList> <callback> |
---|
128 | # |
---|
129 | # Clients use this to prompt the user to upload a file. The string |
---|
130 | # <toolName> is used to identify the application within the web form. |
---|
131 | # The <controlList> is a list of controls that could be uploaded: |
---|
132 | # |
---|
133 | # { <id1> <label1> <desc1> <id2> <label2> <desc2> ... } |
---|
134 | # |
---|
135 | # The user is prompted for each of the controls in <controlList>. |
---|
136 | # If successful, the <callback> is invoked to handle the uploaded |
---|
137 | # information. If anything goes wrong, the same callback is used |
---|
138 | # to post errors near the widget that invoked this operation. |
---|
139 | # |
---|
140 | # The <id> is usually a Rappture path for the control that is being |
---|
141 | # uploaded into. It is passed along to the <callback>, so that code |
---|
142 | # knows where to put the result. If <id> is "@@<dir>", then the file |
---|
143 | # is uploaded into the specified directory <dir> using the name that |
---|
144 | # comes from the remote upload. |
---|
145 | # ---------------------------------------------------------------------- |
---|
146 | proc Rappture::filexfer::upload {tool controlList callback} { |
---|
147 | global env |
---|
148 | variable enabled |
---|
149 | variable commands |
---|
150 | variable job |
---|
151 | |
---|
152 | if {$enabled} { |
---|
153 | set cmd $commands(import) |
---|
154 | if {"" != $tool} { |
---|
155 | lappend cmd --for "for $tool" |
---|
156 | } |
---|
157 | |
---|
158 | set dir ~/data/sessions/$env(SESSION)/spool |
---|
159 | if {![file exists $dir]} { |
---|
160 | catch {file mkdir $dir} |
---|
161 | } |
---|
162 | |
---|
163 | set i 0 |
---|
164 | foreach {path label desc} $controlList { |
---|
165 | set file [file join $dir upload[pid]-[incr i]] |
---|
166 | set file2path($file) $path |
---|
167 | set file2label($file) $label |
---|
168 | lappend cmd --label $label $file |
---|
169 | } |
---|
170 | |
---|
171 | uplevel #0 [list $callback error "Upload starting...\nA web browser page should pop up on your desktop. Use that form to handle the upload operation.\n\nIf the upload form doesn't pop up, make sure that you're allowing pop ups from this site. If it still doesn't pop up, you may be having trouble with the version of Java installed for your browser. See our Support area for details.\n\nClick anywhere to dismiss this message."] |
---|
172 | |
---|
173 | set job(output) "" |
---|
174 | set job(error) "" |
---|
175 | |
---|
176 | set status [catch {eval \ |
---|
177 | blt::bgexec ::Rappture::filexfer::job(control) \ |
---|
178 | -output ::Rappture::filexfer::job(output) \ |
---|
179 | -error ::Rappture::filexfer::job(error) \ |
---|
180 | $cmd |
---|
181 | } result] |
---|
182 | |
---|
183 | if {$status == 0} { |
---|
184 | set changed "" |
---|
185 | set errs "" |
---|
186 | foreach line [split $job(output) \n] { |
---|
187 | # the new filexfer reports "file <= desktopName" |
---|
188 | # the old one just reports "file" |
---|
189 | set remote "" |
---|
190 | if {![regexp {(.+) +<= +(.+)} $line match file remote]} { |
---|
191 | set file [string trim $line] |
---|
192 | } |
---|
193 | |
---|
194 | # load the uploaded for this control |
---|
195 | set status [catch { |
---|
196 | set fid [open $file r] |
---|
197 | fconfigure $fid -translation binary -encoding binary |
---|
198 | set info [read $fid] |
---|
199 | close $fid |
---|
200 | if {[string match @@* $file2path($file)]} { |
---|
201 | if {$remote ne "" && $remote ne "@CLIPBOARD"} { |
---|
202 | set dir [string range $file2path($file) 2 end] |
---|
203 | if {$dir eq ""} { set dir [pwd] } |
---|
204 | set newloc [file join $dir $remote] |
---|
205 | file rename -force -- $file $newloc |
---|
206 | } |
---|
207 | } else { |
---|
208 | file delete -force $file |
---|
209 | } |
---|
210 | } result] |
---|
211 | |
---|
212 | if {$status != 0} { |
---|
213 | append errs "Error loading data for \"$file2label($file)\":\n$result\n" |
---|
214 | } else { |
---|
215 | lappend changed $file2path($file) |
---|
216 | set cmd [list $callback path $file2path($file) local $file data $info] |
---|
217 | if {$remote ne ""} { |
---|
218 | lappend cmd remote $remote |
---|
219 | } |
---|
220 | uplevel #0 $cmd |
---|
221 | } |
---|
222 | } |
---|
223 | if {[llength $changed] == 0} { |
---|
224 | set errs "The form was empty, so none of your controls were changed. In order to upload a file, you must select the file name--or enter text into the copy/paste area--before pushing the Upload button." |
---|
225 | } |
---|
226 | if {[string length $errs] > 0} { |
---|
227 | uplevel #0 [list $callback error $errs] |
---|
228 | } |
---|
229 | } else { |
---|
230 | uplevel #0 [list $callback error $job(error)] |
---|
231 | } |
---|
232 | } else { |
---|
233 | # |
---|
234 | # Filexfer via importfile is disabled. This tool must be |
---|
235 | # running in a desktop environment. Instead of uploading, |
---|
236 | # perform a "Load File..." operation with a standard file |
---|
237 | # selection dialog. |
---|
238 | # |
---|
239 | set i 0 |
---|
240 | foreach {path label desc} $controlList { |
---|
241 | set file [tk_getOpenFile -title "Load File: $label"] |
---|
242 | if {"" != $file} { |
---|
243 | set cmds { |
---|
244 | # try to read first as binary |
---|
245 | set fid [open $file r] |
---|
246 | fconfigure $fid -encoding binary -translation binary |
---|
247 | set info [read $fid] |
---|
248 | close $fid |
---|
249 | if {![Rappture::encoding::is binary $info]} { |
---|
250 | # not binary? then re-read and translate CR/LF |
---|
251 | set fid [open $file r] |
---|
252 | set info [read $fid] |
---|
253 | close $fid |
---|
254 | } |
---|
255 | } |
---|
256 | if {[catch $cmds err]} { |
---|
257 | uplevel #0 [list $callback error "Error loading file [file tail $file]: $err"] |
---|
258 | } else { |
---|
259 | uplevel #0 [list $callback path $path data $info filename $file] |
---|
260 | } |
---|
261 | } |
---|
262 | } |
---|
263 | } |
---|
264 | } |
---|
265 | |
---|
266 | # ---------------------------------------------------------------------- |
---|
267 | # USAGE: Rappture::filexfer::download <string> ?<filename>? |
---|
268 | # |
---|
269 | # Clients use this to send a file off to the user. The <string> |
---|
270 | # is stored in a file called <filename> in the user's spool directory. |
---|
271 | # If there is already a file by that name, then the name is modified |
---|
272 | # to make it unique. Once the string has been stored in the file, |
---|
273 | # a message is sent to all clients listening, letting them know |
---|
274 | # that the file is available. |
---|
275 | # |
---|
276 | # If anything goes wrong, this function returns a string that should |
---|
277 | # be displayed to the user to explain the problem. |
---|
278 | # ---------------------------------------------------------------------- |
---|
279 | proc Rappture::filexfer::download {string {filename "output.txt"}} { |
---|
280 | global env |
---|
281 | variable enabled |
---|
282 | variable commands |
---|
283 | variable job |
---|
284 | |
---|
285 | if {$enabled} { |
---|
286 | # make a spool directory, if we don't have one already |
---|
287 | set dir ~/data/sessions/$env(SESSION)/spool |
---|
288 | if {![file exists $dir]} { |
---|
289 | catch {file mkdir $dir} |
---|
290 | } |
---|
291 | |
---|
292 | if {[file exists [file join $dir $filename]]} { |
---|
293 | # |
---|
294 | # Find a similar file name that doesn't conflict |
---|
295 | # with an existing file: e.g., output2.txt |
---|
296 | # |
---|
297 | set root [file rootname $filename] |
---|
298 | set ext [file extension $filename] |
---|
299 | set counter 2 |
---|
300 | while {1} { |
---|
301 | set filename "$root$counter$ext" |
---|
302 | if {![file exists [file join $dir $filename]]} { |
---|
303 | break |
---|
304 | } |
---|
305 | incr counter |
---|
306 | } |
---|
307 | } |
---|
308 | |
---|
309 | # |
---|
310 | # Save the file in the spool directory, then have it |
---|
311 | # exported. |
---|
312 | # |
---|
313 | if {[catch { |
---|
314 | set file [file join $dir $filename] |
---|
315 | set fid [open $file w] |
---|
316 | fconfigure $fid -encoding binary -translation binary |
---|
317 | puts -nonewline $fid $string |
---|
318 | close $fid |
---|
319 | } result]} { |
---|
320 | return $result |
---|
321 | } |
---|
322 | |
---|
323 | set job(output) "" |
---|
324 | set job(error) "" |
---|
325 | |
---|
326 | # Make sure we normalize the file name here. Don't count |
---|
327 | # on filexfer command understanding tildes. |
---|
328 | set file [file normalize $file] |
---|
329 | |
---|
330 | set status [catch {blt::bgexec ::Rappture::filexfer::job(control) \ |
---|
331 | -output ::Rappture::filexfer::job(output) \ |
---|
332 | -error ::Rappture::filexfer::job(error) \ |
---|
333 | $commands(export) --timeout 300 --delete $file} result] |
---|
334 | |
---|
335 | if {$status != 0} { |
---|
336 | return $Rappture::filexfer::job(error) |
---|
337 | } |
---|
338 | } else { |
---|
339 | # |
---|
340 | # Filexfer via exportfile is disabled. This tool must be |
---|
341 | # running in a desktop environment. Instead of downloading, |
---|
342 | # perform a "Save As..." operation with a standard file |
---|
343 | # selection dialog. |
---|
344 | # |
---|
345 | set file [tk_getSaveFile -title "Save As..." -initialfile $filename] |
---|
346 | # FIXME: Why is there a grab still pending? |
---|
347 | set grab [grab current] |
---|
348 | if { $grab != "" } { |
---|
349 | grab release $grab |
---|
350 | } |
---|
351 | if {"" != $file} { |
---|
352 | if {[catch { |
---|
353 | set fid [open $file w] |
---|
354 | if {[Rappture::encoding::is binary $string]} { |
---|
355 | fconfigure $fid -encoding binary -translation binary |
---|
356 | puts -nonewline $fid $string |
---|
357 | } else { |
---|
358 | puts $fid $string |
---|
359 | } |
---|
360 | close $fid |
---|
361 | } result]} { |
---|
362 | return $result |
---|
363 | } |
---|
364 | } |
---|
365 | } |
---|
366 | return "" |
---|
367 | } |
---|
368 | |
---|
369 | # ---------------------------------------------------------------------- |
---|
370 | # USAGE: Rappture::filexfer::webpage <url> |
---|
371 | # |
---|
372 | # Clients use this to pop up a web page for the specified <url> on |
---|
373 | # the user's desktop. The <url> should start with http:// or https://. |
---|
374 | # If anything goes wrong, this function beeps. |
---|
375 | # ---------------------------------------------------------------------- |
---|
376 | proc Rappture::filexfer::webpage {url} { |
---|
377 | if {[regexp -nocase {^https?://} $url]} { |
---|
378 | foreach prog { |
---|
379 | clientaction |
---|
380 | /usr/lib/hubzero/clientaction |
---|
381 | /apps/bin/clientaction |
---|
382 | /apps/xvnc/bin/clientaction |
---|
383 | /usr/lib/mw/bin/clientaction |
---|
384 | "" |
---|
385 | } { |
---|
386 | if {"" != [auto_execok $prog]} { |
---|
387 | break |
---|
388 | } |
---|
389 | } |
---|
390 | if {"" != $prog} { |
---|
391 | exec $prog url $url & |
---|
392 | return |
---|
393 | } |
---|
394 | } |
---|
395 | bell |
---|
396 | } |
---|