CCloadItemData.swift 11 KB

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