NCUploadAssets.swift 13 KB

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