NCUploadAssets.swift 14 KB

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