NCUploadAssets.swift 14 KB

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