NCUploadAssets.swift 14 KB

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