//
// D o w n l o a d F r o m R S t u d i o S e r v e r . s w i f t
// R S w i t c h
//
// C r e a t e d b y h r b r m s t r o n 8 / 2 4 / 1 9 .
// C o p y r i g h t © 2 0 1 9 B o b R u d i s . A l l r i g h t s r e s e r v e d .
//
import Foundation
import Cocoa
func download_from_studio_server ( fromRS : String , toFS : String ) {
NSLog ( " download from rstudio server " )
let rsURL = URL ( string : fromRS ) !
let fsURL = URL ( string : toFS ) !
URLSession . shared . configuration . timeoutIntervalForRequest = 300.0
// U R L S e s s i o n . s h a r e d . d o w n l o a d T a s k ( w i t h : < # T # # U R L R e q u e s t # > )
let task = URLSession . shared . downloadTask ( with : rsURL ) {
localURL , urlResponse , error in
if ( error != nil ) {
NSLog ( " dler \( String ( describing : error ) ) " )
} else {
if let localURL = localURL {
NSLog ( " We've got the data " ) ;
if ( FileManager . default . fileExists ( atPath : fsURL . path ) ) {
NSLog ( " Deleting old file " )
do {
try FileManager . default . removeItem ( at : fsURL )
} catch {
NSLog ( " error deleting old file " )
}
}
do {
NSLog ( " Trying to move the data from \( localURL ) to \( fsURL ) " ) ;
try FileManager . default . moveItem ( at : localURL , to : fsURL )
NSWorkspace . shared . openFile (
fsURL . deletingLastPathComponent ( ) . absoluteString , withApplication : " Finder "
)
NSWorkspace . shared . activateFileViewerSelecting ( [ fsURL ] )
} catch {
NSLog ( " Move Error \( error ) " )
}
}
}
}
task . resume ( )
}