NCUploadAssets.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. //
  2. // NCUploadAssets.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 04/01/23.
  6. // Copyright © 2023 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 SwiftUI
  24. import NextcloudKit
  25. class NCHostingUploadAssetsView: NSObject {
  26. @objc func makeShipDetailsUI(assets: [PHAsset], cryptated: Bool, session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) -> UIViewController {
  27. let uploadAssets = NCUploadAssets(assets: assets, session: session, userBaseUrl: userBaseUrl, serverUrl: serverUrl)
  28. let details = UploadAssetsView(uploadAssets: uploadAssets, serverUrl: serverUrl)
  29. let vc = UIHostingController(rootView: details)
  30. return vc
  31. }
  32. }
  33. class NCUploadAssets: ObservableObject {
  34. internal var assets: [PHAsset]
  35. internal var session: String
  36. internal var userBaseUrl: NCUserBaseUrl
  37. @Published var serverUrl: String
  38. @Published var isMaintainOriginalFilename: Bool = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
  39. @Published var isAddFilenametype: Bool = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
  40. init(assets: [PHAsset], session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) {
  41. self.assets = assets
  42. self.session = session
  43. self.userBaseUrl = userBaseUrl
  44. self.serverUrl = serverUrl
  45. }
  46. func setFileNameMask(fileName: String?) -> String {
  47. guard let asset = assets.first else { return "" }
  48. var preview: String = ""
  49. let creationDate = asset.creationDate ?? Date()
  50. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  51. if let fileName = fileName {
  52. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  53. if !fileName.isEmpty {
  54. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  55. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  56. fileDate: creationDate, fileType: asset.mediaType,
  57. keyFileName: NCGlobal.shared.keyFileNameMask,
  58. keyFileNameType: NCGlobal.shared.keyFileNameType,
  59. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  60. forcedNewFileName: false)
  61. } else {
  62. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  63. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  64. fileDate: creationDate,
  65. fileType: asset.mediaType,
  66. keyFileName: nil,
  67. keyFileNameType: NCGlobal.shared.keyFileNameType,
  68. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  69. forcedNewFileName: false)
  70. }
  71. } else {
  72. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  73. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  74. fileDate: creationDate,
  75. fileType: asset.mediaType,
  76. keyFileName: nil,
  77. keyFileNameType: NCGlobal.shared.keyFileNameType,
  78. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  79. forcedNewFileName: false)
  80. }
  81. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + preview
  82. }
  83. func getOriginalFilename() -> String {
  84. if let asset = assets.first, let name = (asset.value(forKey: "filename") as? String) {
  85. return name
  86. } else {
  87. return ""
  88. }
  89. }
  90. func save(completion: @escaping (_ openConflictViewController: Bool) -> Void) {
  91. var metadatasNOConflict: [tableMetadata] = []
  92. var metadatasUploadInConflict: [tableMetadata] = []
  93. let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: userBaseUrl.urlBase, userId: userBaseUrl.userId, account: userBaseUrl.account)
  94. for asset in self.assets {
  95. var serverUrl = self.serverUrl
  96. var livePhoto: Bool = false
  97. let creationDate = asset.creationDate ?? Date()
  98. let fileName = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  99. fileDate: creationDate,
  100. fileType: asset.mediaType,
  101. keyFileName: NCGlobal.shared.keyFileNameMask,
  102. keyFileNameType: NCGlobal.shared.keyFileNameType,
  103. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  104. forcedNewFileName: false)!
  105. if asset.mediaSubtypes.contains(.photoLive) && CCUtility.getLivePhoto() {
  106. livePhoto = true
  107. }
  108. // Check if is in upload
  109. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  110. if !isRecordInSessions.isEmpty { continue }
  111. let metadataForUpload = NCManageDatabase.shared.createMetadata(account: userBaseUrl.account, user: userBaseUrl.user, userId: userBaseUrl.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: serverUrl, urlBase: userBaseUrl.urlBase, url: "", contentType: "", isLivePhoto: livePhoto)
  112. metadataForUpload.assetLocalIdentifier = asset.localIdentifier
  113. metadataForUpload.session = self.session
  114. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  115. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  116. if let result = NCManageDatabase.shared.getMetadataConflict(account: userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
  117. metadataForUpload.fileName = result.fileName
  118. metadatasUploadInConflict.append(metadataForUpload)
  119. } else {
  120. metadatasNOConflict.append(metadataForUpload)
  121. }
  122. }
  123. // Verify if file(s) exists
  124. if !metadatasUploadInConflict.isEmpty {
  125. /*
  126. DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
  127. if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
  128. conflict.serverUrl = self.serverUrl
  129. conflict.metadatasNOConflict = metadatasNOConflict
  130. conflict.metadatasUploadInConflict = metadatasUploadInConflict
  131. conflict.delegate = self.appDelegate
  132. self.appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
  133. }
  134. }
  135. */
  136. completion(true)
  137. } else {
  138. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatasNOConflict, completion: { _ in })
  139. completion(false)
  140. }
  141. }
  142. }
  143. // MARK: - Delegate
  144. extension NCUploadAssets: NCSelectDelegate {
  145. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
  146. if let serverUrl = serverUrl {
  147. CCUtility.setDirectoryScanDocument(serverUrl)
  148. self.serverUrl = serverUrl
  149. }
  150. }
  151. }
  152. extension NCUploadAssets: NCCreateFormUploadConflictDelegate {
  153. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  154. /*
  155. if let metadata = metadatas?.first {
  156. self.showHUD.toggle()
  157. createPDF(metadata: metadata) { error in
  158. if !error {
  159. self.showHUD.toggle()
  160. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDismissScanDocument)
  161. }
  162. }
  163. }
  164. */
  165. }
  166. }
  167. // MARK: - View
  168. struct UploadAssetsView: View {
  169. @State private var serverUrl: String
  170. @State var fileName: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
  171. @State var isPresentedSelect = false
  172. @State var isPresentedUploadConflict = false
  173. @ObservedObject var uploadAssets: NCUploadAssets
  174. init(uploadAssets: NCUploadAssets, serverUrl: String) {
  175. self.uploadAssets = uploadAssets
  176. self.serverUrl = serverUrl
  177. }
  178. var body: some View {
  179. NavigationView {
  180. List {
  181. Section(header: Text(NSLocalizedString("_save_path_", comment: ""))) {
  182. HStack {
  183. Label {
  184. if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == serverUrl {
  185. Text("/")
  186. .frame(maxWidth: .infinity, alignment: .trailing)
  187. } else {
  188. Text((serverUrl as NSString).lastPathComponent)
  189. .frame(maxWidth: .infinity, alignment: .trailing)
  190. }
  191. } icon: {
  192. Image("folder")
  193. .renderingMode(.template)
  194. .resizable()
  195. .scaledToFit()
  196. .foregroundColor(Color(NCBrandColor.shared.brand))
  197. }
  198. }
  199. .contentShape(Rectangle())
  200. .onTapGesture {
  201. isPresentedSelect = true
  202. }
  203. }
  204. Section(header: Text(NSLocalizedString("_mode_filename_", comment: ""))) {
  205. Toggle(NSLocalizedString("_maintain_original_filename_", comment: ""), isOn: $uploadAssets.isMaintainOriginalFilename)
  206. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  207. .onChange(of: uploadAssets.isMaintainOriginalFilename) { _ in }
  208. Toggle(NSLocalizedString("_add_filenametype_", comment: ""), isOn: $uploadAssets.isAddFilenametype)
  209. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  210. .onChange(of: uploadAssets.isAddFilenametype) { _ in }
  211. }
  212. Section(header: Text(NSLocalizedString("_filename_", comment: ""))) {
  213. HStack {
  214. Text(NSLocalizedString("_filename_", comment: ""))
  215. if uploadAssets.isMaintainOriginalFilename {
  216. Text(uploadAssets.getOriginalFilename())
  217. .frame(maxWidth: .infinity, alignment: .trailing)
  218. } else {
  219. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
  220. .modifier(TextFieldClearButton(text: $fileName))
  221. .multilineTextAlignment(.trailing)
  222. }
  223. }
  224. if !uploadAssets.isMaintainOriginalFilename {
  225. Text(uploadAssets.setFileNameMask(fileName: fileName))
  226. }
  227. }
  228. .complexModifier { view in
  229. if #available(iOS 15, *) {
  230. view.listRowSeparator(.hidden)
  231. }
  232. }
  233. Button(NSLocalizedString("_save_", comment: "")) {
  234. }
  235. .frame(maxWidth: .infinity)
  236. .buttonStyle(ButtonRounded(disabled: false))
  237. .listRowBackground(Color(UIColor.systemGroupedBackground))
  238. }
  239. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  240. .navigationBarTitleDisplayMode(.inline)
  241. }
  242. .sheet(isPresented: $isPresentedSelect) {
  243. NCSelectView(serverUrl: $serverUrl)
  244. }
  245. .sheet(isPresented: $isPresentedUploadConflict) {
  246. if let serverUrl = serverUrl {
  247. NCUploadConflictRepresentedView(delegate: uploadAssets, serverUrl: serverUrl, metadatasUploadInConflict: []) // uploadAssets.metadata
  248. }
  249. }
  250. }
  251. }
  252. // MARK: - Preview
  253. struct UploadAssetsView_Previews: PreviewProvider {
  254. static var previews: some View {
  255. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  256. let uploadAssets = NCUploadAssets(assets: [], session: "", userBaseUrl: appDelegate, serverUrl: "/")
  257. UploadAssetsView(uploadAssets: uploadAssets, serverUrl: "/")
  258. }
  259. }
  260. }