CCloadItemData.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. //
  2. // CCloadItemData.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 19/02/16.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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. @objc func loadFiles(_ directory: String, extensionContext: NSExtensionContext, vc: ShareViewController) {
  27. var filesName: [String] = []
  28. var conuter = 0
  29. let hud = CCHud(view: vc.view)!
  30. CCUtility.clearTmpDirectory()
  31. if let inputItems : [NSExtensionItem] = extensionContext.inputItems as? [NSExtensionItem] {
  32. for item : NSExtensionItem in inputItems {
  33. if let attachments = item.attachments {
  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. var fileNameOriginal: String?
  44. var fileName: String = ""
  45. let dateFormatter = DateFormatter()
  46. dateFormatter.dateFormat = "yyyy-MM-dd HH-mm-ss-"
  47. conuter += 1
  48. if let url = item as? NSURL {
  49. fileNameOriginal = url.lastPathComponent!
  50. }
  51. if error == nil {
  52. if let image = item as? UIImage {
  53. print("item as UIImage")
  54. if let pngImageData = image.pngData() {
  55. if fileNameOriginal != nil {
  56. fileName = fileNameOriginal!
  57. } else {
  58. fileName = "\(dateFormatter.string(from: Date()))\(conuter).png"
  59. }
  60. let filenamePath = directory + fileName
  61. let result = (try? pngImageData.write(to: URL(fileURLWithPath: filenamePath), options: [.atomic])) != nil
  62. if result {
  63. filesName.append(fileName)
  64. }
  65. } else {
  66. print("Error image nil")
  67. }
  68. }
  69. if let url = item as? URL {
  70. print("item as url: \(String(describing: item))")
  71. if fileNameOriginal != nil {
  72. fileName = fileNameOriginal!
  73. } else {
  74. let ext = url.pathExtension
  75. fileName = "\(dateFormatter.string(from: Date()))\(conuter)." + ext
  76. }
  77. let filenamePath = directory + fileName
  78. do {
  79. try FileManager.default.removeItem(atPath: filenamePath)
  80. }
  81. catch let error as NSError {
  82. print("Ooops! Something went wrong: \(error)")
  83. }
  84. do {
  85. try FileManager.default.copyItem(atPath: url.path, toPath:filenamePath)
  86. do {
  87. let attr : NSDictionary? = try FileManager.default.attributesOfItem(atPath: filenamePath) as NSDictionary?
  88. if let _attr = attr {
  89. if _attr.fileSize() > 0 {
  90. filesName.append(fileName)
  91. }
  92. }
  93. } catch let error as NSError {
  94. print("Error: \(error.localizedDescription)")
  95. }
  96. } catch let error as NSError {
  97. print("Cannot copy file: \(error.localizedDescription)")
  98. }
  99. }
  100. if let data = item as? Data {
  101. if data.count > 0 {
  102. print("item as NSdata")
  103. if fileNameOriginal != nil {
  104. fileName = fileNameOriginal!
  105. } else {
  106. let description = current.description
  107. let fullNameArr = description.components(separatedBy: "\"")
  108. let fileExtArr = fullNameArr[1].components(separatedBy: ".")
  109. let pathExtention = (fileExtArr[fileExtArr.count-1]).uppercased()
  110. fileName = "\(dateFormatter.string(from: Date()))\(conuter).\(pathExtention)"
  111. }
  112. let filenamePath = directory + fileName
  113. FileManager.default.createFile(atPath: filenamePath, contents:data, attributes:nil)
  114. filesName.append(fileName)
  115. }
  116. }
  117. if let data = item as? NSString {
  118. if data.length > 0 {
  119. print("item as NSString")
  120. let fileName = "\(dateFormatter.string(from: Date()))\(conuter).txt"
  121. let filenamePath = directory + fileName
  122. FileManager.default.createFile(atPath: filenamePath, contents:data.data(using: String.Encoding.utf8.rawValue), attributes:nil)
  123. filesName.append(fileName)
  124. }
  125. }
  126. if index + 1 == attachments.count {
  127. vc.performSelector(onMainThread: #selector(vc.reloadData), with:filesName, waitUntilDone: false)
  128. hud.performSelector(onMainThread: #selector(CCHud.hideHud), with: nil, waitUntilDone: false)
  129. }
  130. } else {
  131. hud.performSelector(onMainThread: #selector(CCHud.hideHud), with: nil, waitUntilDone: false)
  132. }
  133. })
  134. }
  135. } // end for
  136. } else {
  137. vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
  138. }
  139. }
  140. } else {
  141. vc.performSelector(onMainThread: #selector(vc.close), with: nil, waitUntilDone: false);
  142. }
  143. }
  144. }