NCUploadAssets.swift 12 KB

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