Advertisement
Guest User

Untitled

a guest
Jun 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. module EasySyncClient.AlfrescoClient
  2.  
  3. open NLog
  4. open WebDAVClient
  5. open EasySyncClient.ClientModels
  6. open EasySyncClient.Models
  7. open System.Net
  8. open System.IO
  9. open System.Threading.Tasks
  10. open EasySyncClient.Utility
  11. open System
  12.  
  13. type UpdateResult =
  14. | Success
  15. | Failed of string * string
  16. | Skip
  17.  
  18. type AlfrescoClient(endPoint) =
  19. let client = Client(NetworkCredential(UserName = endPoint.User, Password = endPoint.Password))
  20. do client.Server <- endPoint.Url
  21.  
  22. member this.TryCreateDirectory remoteRoot remotePath =
  23. let full = createFullRemotePath remoteRoot remotePath |> fun (FullRemotePath path) -> path
  24.  
  25. async {
  26. try
  27. do! Task.Delay(1000) |> Async.AwaitTask
  28. log "try to create directory %s" full
  29. let! items = client.GetFolder full |> Async.AwaitTask
  30. return Success
  31. with ex ->
  32. try
  33. let path, last = extractFullRemotePath (FullRemotePath full)
  34. log "create %s => %s" path last
  35. client.CreateDir(path, last) |> Async.AwaitTask |> ignore
  36. return Success
  37. with
  38. | ex -> return Failed (ex.Message, ex.StackTrace)
  39. } |> Async.RunSynchronously
  40.  
  41. member this.TryCreateDirectories remoteRoot remotePath =
  42. async {
  43. let full = createFullRemotePathNoName remoteRoot remotePath |> fun (FullRemotePathNoName path) -> path
  44. log "try to create directories %s" full
  45. try
  46. let! item = client.GetFolder full |> Async.AwaitTask
  47. return Success
  48. with
  49. | ex ->
  50. let sections = createRemoteSections remotePath
  51. let createDir = this.TryCreateDirectory remoteRoot
  52. let results = sections |> List.map createDir
  53. return Success
  54. }
  55.  
  56. member this.CanUpdate (FullRemotePath remote) lastModified =
  57. async {
  58. try
  59. log "can update %s" remote
  60. let! file = client.GetFile(remote) |> Async.AwaitTask
  61. log "can update %A" file.LastModified
  62. if file.LastModified.Value < lastModified then
  63. return Success
  64. else
  65. return Skip
  66. with | ex -> return Success
  67. }
  68.  
  69. member this.DeleteFile remoteRoot localRoot fullLocalPath =
  70. async {
  71. let fullRemotePath (FullRemotePath remote) = remote
  72. let remotePath = createRelativeRemotePath localRoot fullLocalPath
  73. let targetPath = createFullRemotePath remoteRoot remotePath
  74. let str = fullRemotePath targetPath
  75. client.DeleteFile(str) |> Async.AwaitTask |> ignore
  76. return Success
  77. }
  78.  
  79. member this.MoveFile remoteRoot localRoot info =
  80. async {
  81. let fullRemotePath (FullRemotePath remote ) = remote
  82. let extractPath localPath =
  83. let path = createRelativeRemotePath localRoot (FullLocalPath localPath)
  84. let targetPath = createFullRemotePath remoteRoot path
  85. fullRemotePath targetPath
  86.  
  87. let newFileName = Path.GetFileName(info.NewPath)
  88. let originalPath = extractPath info.OldPath
  89. let newPath = extractPath info.NewPath
  90.  
  91. log "move %s to %s" originalPath newPath
  92.  
  93. let! rs = client.MoveFile(originalPath, newPath) |> Async.AwaitTask
  94. return Success
  95. }
  96.  
  97. member this.UploadFile remoteRoot localRoot localPath =
  98. let getLocalPath (FullLocalPath path) = path
  99. let relativePath = createRelativeRemotePath localRoot localPath
  100. let getPath (RemoteRelativePath path) = Path.GetDirectoryName path |> RemoteRelativeNoName
  101. let date = File.GetLastWriteTime (getLocalPath localPath)
  102.  
  103. async {
  104. try
  105. let targetPath = createFullRemotePath remoteRoot relativePath
  106. let path, name = extractFullRemotePath targetPath
  107. let! canUpdate = this.CanUpdate targetPath date
  108. match canUpdate with
  109. | Success ->
  110. let! dir = this.TryCreateDirectories remoteRoot (getPath relativePath)
  111. log "try to upload file %s => %s" path name
  112. let! item = client.Upload(path, File.OpenRead(getLocalPath localPath), name) |> Async.AwaitTask
  113. return Success
  114. | Failed (ex, stack) ->
  115. return Failed (ex, stack)
  116. | Skip ->
  117. return Skip
  118. with
  119. | ex -> return Failed (ex.Message, ex.StackTrace)
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement