NCUploadAssets.swift 13 KB

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