CCloadItemData.swift 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. //
  2. // CCloadItemData.swift
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/02/16.
  6. // Copyright (c) 2014 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import MobileCoreServices
  25. class CCloadItemData: NSObject {
  26. func loadFiles(_ directoryUser: String, extensionContext: NSExtensionContext, vc: ShareViewController)
  27. {
  28. var filesName: [String] = []
  29. var conuter = 0
  30. let hud = CCHud(view: vc.view)!
  31. if let inputItems : [NSExtensionItem] = extensionContext.inputItems as? [NSExtensionItem] {
  32. for item : NSExtensionItem in inputItems {
  33. if let attachments = item.attachments as? [NSItemProvider] {
  34. if attachments.isEmpty {
  35. extensionContext.completeRequest(returningItems: nil, completionHandler: nil)
  36. vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
  37. return
  38. }
  39. for (index, current) in (attachments.enumerated()) {
  40. if current.hasItemConformingToTypeIdentifier(kUTTypeItem as String) {
  41. hud.visibleIndeterminateHud()
  42. current.loadItem(forTypeIdentifier: kUTTypeItem as String, options: nil, completionHandler: {(item, error) -> Void in
  43. if error == nil {
  44. let dateFormatter = DateFormatter()
  45. dateFormatter.dateFormat = "yyyy-MM-dd HH-mm-ss-"
  46. conuter += 1
  47. if let image = item as? UIImage {
  48. print("item as UIImage")
  49. if let pngImageData = UIImagePNGRepresentation(image) {
  50. let fileName = "\(dateFormatter.string(from: Date()))\(conuter).png"
  51. let filenamePath = directoryUser + "/" + fileName
  52. let result = (try? pngImageData.write(to: URL(fileURLWithPath: filenamePath), options: [.atomic])) != nil
  53. if result {
  54. filesName.append(fileName)
  55. }
  56. } else {
  57. print("Error image nil")
  58. }
  59. }
  60. if let url = item as? URL {
  61. print("item as url: \(String(describing: item))")
  62. let pathExtention = URL(fileURLWithPath: url.lastPathComponent).pathExtension
  63. let fileName = "\(dateFormatter.string(from: Date()))\(conuter).\(pathExtention)"
  64. let filenamePath = directoryUser + "/" + fileName
  65. do {
  66. try FileManager.default.copyItem(atPath: url.path, toPath:filenamePath)
  67. do {
  68. let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filenamePath) as NSDictionary?
  69. if let _attr = attr {
  70. if _attr.fileSize() > 0 {
  71. filesName.append(fileName)
  72. }
  73. }
  74. } catch let error as NSError {
  75. print("Error: \(error.localizedDescription)")
  76. }
  77. } catch let error as NSError {
  78. print("Cannot copy file: \(error.localizedDescription)")
  79. }
  80. }
  81. if let data = item as? Data {
  82. if data.count > 0 {
  83. print("item as NSdata")
  84. let description = current.description
  85. let fullNameArr = description.components(separatedBy: "\"")
  86. let fileExtArr = fullNameArr[1].components(separatedBy: ".")
  87. let pathExtention = (fileExtArr[fileExtArr.count-1]).uppercased()
  88. let fileName = "\(dateFormatter.string(from: Date()))\(conuter).\(pathExtention)"
  89. let filenamePath = directoryUser + "/" + fileName
  90. FileManager.default.createFile(atPath: filenamePath, contents:data, attributes:nil)
  91. filesName.append(fileName)
  92. }
  93. }
  94. if let data = item as? NSString {
  95. if data.length > 0 {
  96. print("item as NSString")
  97. let fileName = "\(dateFormatter.string(from: Date()))\(conuter).txt"
  98. let filenamePath = directoryUser + "/" + fileName
  99. FileManager.default.createFile(atPath: filenamePath, contents:data.data(using: String.Encoding.utf8.rawValue), attributes:nil)
  100. filesName.append(fileName)
  101. }
  102. }
  103. if index + 1 == attachments.count {
  104. vc.performSelector(onMainThread: #selector(vc.reloadData), with:filesName, waitUntilDone: false)
  105. hud.performSelector(onMainThread: #selector(CCHud.hideHud), with: nil, waitUntilDone: false)
  106. }
  107. } else {
  108. print("ERROR: \(error)")
  109. hud.performSelector(onMainThread: #selector(CCHud.hideHud), with: nil, waitUntilDone: false)
  110. }
  111. })
  112. }
  113. } // end for
  114. } else {
  115. vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
  116. }
  117. }
  118. } else {
  119. vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
  120. }
  121. }
  122. }