NCUploadAssets.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. @State private var metadatasNOConflict: [tableMetadata]
  54. @State private var metadatasUploadInConflict: [tableMetadata]
  55. @State private var metadatasToUpload: [tableMetadata]
  56. init(uploadAssets: NCUploadAssets) {
  57. self.uploadAssets = uploadAssets
  58. }
  59. func getOriginalFilename() -> String {
  60. if let asset = uploadAssets.assets.first, let name = (asset.value(forKey: "filename") as? String) {
  61. return name
  62. } else {
  63. return ""
  64. }
  65. }
  66. func setFileNameMask(fileName: String?) -> String {
  67. guard let asset = uploadAssets.assets.first else { return "" }
  68. var preview: String = ""
  69. let creationDate = asset.creationDate ?? Date()
  70. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  71. if let fileName = fileName {
  72. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  73. if !fileName.isEmpty {
  74. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  75. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  76. fileDate: creationDate, fileType: asset.mediaType,
  77. keyFileName: NCGlobal.shared.keyFileNameMask,
  78. keyFileNameType: NCGlobal.shared.keyFileNameType,
  79. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  80. forcedNewFileName: false)
  81. } else {
  82. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  83. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  84. fileDate: creationDate,
  85. fileType: asset.mediaType,
  86. keyFileName: nil,
  87. keyFileNameType: NCGlobal.shared.keyFileNameType,
  88. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  89. forcedNewFileName: false)
  90. }
  91. } else {
  92. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  93. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  94. fileDate: creationDate,
  95. fileType: asset.mediaType,
  96. keyFileName: nil,
  97. keyFileNameType: NCGlobal.shared.keyFileNameType,
  98. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  99. forcedNewFileName: false)
  100. }
  101. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + preview
  102. }
  103. func save(completion: @escaping (_ metadatasNOConflict: [tableMetadata], _ metadatasUploadInConflict: [tableMetadata]) -> Void) {
  104. var metadatasNOConflict: [tableMetadata] = []
  105. var metadatasUploadInConflict: [tableMetadata] = []
  106. for asset in uploadAssets.assets {
  107. let serverUrl = uploadAssets.serverUrl
  108. var livePhoto: Bool = false
  109. let creationDate = asset.creationDate ?? Date()
  110. let fileName = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  111. fileDate: creationDate,
  112. fileType: asset.mediaType,
  113. keyFileName: NCGlobal.shared.keyFileNameMask,
  114. keyFileNameType: NCGlobal.shared.keyFileNameType,
  115. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  116. forcedNewFileName: false)!
  117. if asset.mediaSubtypes.contains(.photoLive) && CCUtility.getLivePhoto() {
  118. livePhoto = true
  119. }
  120. // Check if is in upload
  121. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", uploadAssets.userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  122. if !isRecordInSessions.isEmpty { continue }
  123. 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)
  124. metadataForUpload.assetLocalIdentifier = asset.localIdentifier
  125. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  126. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  127. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  128. if let result = NCManageDatabase.shared.getMetadataConflict(account: uploadAssets.userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
  129. metadataForUpload.fileName = result.fileName
  130. metadatasUploadInConflict.append(metadataForUpload)
  131. } else {
  132. metadatasNOConflict.append(metadataForUpload)
  133. }
  134. }
  135. // Verify if file(s) exists
  136. if !metadatasUploadInConflict.isEmpty {
  137. completion(metadatasNOConflict, metadatasUploadInConflict)
  138. } else {
  139. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatasNOConflict, completion: { _ in })
  140. completion(metadatasNOConflict, metadatasUploadInConflict)
  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: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == uploadAssets.serverUrl {
  150. Text("/")
  151. .frame(maxWidth: .infinity, alignment: .trailing)
  152. } else {
  153. Text((uploadAssets.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. save { metadatasNOConflict, metadatasUploadInConflict in
  200. if metadatasUploadInConflict.isEmpty {
  201. self.presentationMode.wrappedValue.dismiss()
  202. } else {
  203. self.metadatasNOConflict = metadatasNOConflict
  204. self.metadatasUploadInConflict = metadatasUploadInConflict
  205. }
  206. }
  207. }
  208. .frame(maxWidth: .infinity)
  209. .buttonStyle(ButtonRounded(disabled: false))
  210. .listRowBackground(Color(UIColor.systemGroupedBackground))
  211. }
  212. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  213. .navigationBarTitleDisplayMode(.inline)
  214. }
  215. .sheet(isPresented: $isPresentedSelect) {
  216. SelectView(serverUrl: $uploadAssets.serverUrl)
  217. }
  218. .sheet(isPresented: $isPresentedUploadConflict) {
  219. }
  220. }
  221. }
  222. // MARK: - Preview
  223. struct UploadAssetsView_Previews: PreviewProvider {
  224. static var previews: some View {
  225. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  226. let uploadAssets = NCUploadAssets(assets: [], serverUrl: "/", userBaseUrl: appDelegate)
  227. UploadAssetsView(uploadAssets: uploadAssets)
  228. }
  229. }
  230. }