NCUploadAssets.swift 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. import Mantis
  27. import Photos
  28. import QuickLook
  29. @available(iOS 15, *)
  30. class NCHostingUploadAssetsView: NSObject {
  31. func makeShipDetailsUI(assets: [TLPHAsset], serverUrl: String, userBaseUrl: NCUserBaseUrl) -> UIViewController {
  32. let uploadAssets = NCUploadAssets(assets: assets, serverUrl: serverUrl, userBaseUrl: userBaseUrl )
  33. let details = UploadAssetsView(uploadAssets: uploadAssets)
  34. return UIHostingController(rootView: details)
  35. }
  36. }
  37. // MARK: - Class
  38. struct PreviewStore {
  39. var id: String
  40. var asset: TLPHAsset
  41. var assetType: TLPHAsset.AssetType
  42. var data: Data?
  43. var fileName: String
  44. var image: UIImage
  45. }
  46. class NCUploadAssets: NSObject, ObservableObject, NCCreateFormUploadConflictDelegate {
  47. @Published var serverUrl: String
  48. @Published var assets: [TLPHAsset]
  49. @Published var userBaseUrl: NCUserBaseUrl
  50. @Published var dismiss = false
  51. @Published var isUseAutoUploadFolder: Bool = false
  52. @Published var isUseAutoUploadSubFolder: Bool = false
  53. @Published var previewStore: [PreviewStore] = []
  54. @Published var showHUD: Bool = false
  55. @Published var uploadInProgress: Bool = false
  56. var metadatasNOConflict: [tableMetadata] = []
  57. var metadatasUploadInConflict: [tableMetadata] = []
  58. var timer: Timer?
  59. init(assets: [TLPHAsset], serverUrl: String, userBaseUrl: NCUserBaseUrl) {
  60. self.assets = assets
  61. self.serverUrl = serverUrl
  62. self.userBaseUrl = userBaseUrl
  63. }
  64. func loadImages() {
  65. var previewStore: [PreviewStore] = []
  66. DispatchQueue.global().async {
  67. for asset in self.assets {
  68. guard let image = asset.fullResolutionImage?.resizeImage(size: CGSize(width: 300, height: 300), isAspectRation: true), let localIdentifier = asset.phAsset?.localIdentifier else { continue }
  69. previewStore.append(PreviewStore(id: localIdentifier, asset: asset, assetType: asset.type, fileName: "", image: image))
  70. }
  71. DispatchQueue.main.async {
  72. self.previewStore = previewStore
  73. }
  74. }
  75. }
  76. func startTimer(navigationItem: UINavigationItem) {
  77. self.timer = Timer.scheduledTimer(withTimeInterval: 0.5, repeats: true, block: { _ in
  78. guard let buttonDone = navigationItem.leftBarButtonItems?.first, let buttonCrop = navigationItem.leftBarButtonItems?.last else { return }
  79. buttonCrop.isEnabled = true
  80. buttonDone.isEnabled = true
  81. if let markup = navigationItem.rightBarButtonItems?.first(where: { $0.accessibilityIdentifier == "QLOverlayMarkupButtonAccessibilityIdentifier" }) {
  82. if let originalButton = markup.value(forKey: "originalButton") as AnyObject? {
  83. if let symbolImageName = originalButton.value(forKey: "symbolImageName") as? String {
  84. if symbolImageName == "pencil.tip.crop.circle.on" {
  85. buttonCrop.isEnabled = false
  86. buttonDone.isEnabled = false
  87. }
  88. }
  89. }
  90. }
  91. })
  92. }
  93. func stopTimer() {
  94. self.timer?.invalidate()
  95. }
  96. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  97. guard let metadatas = metadatas else {
  98. self.showHUD = false
  99. self.uploadInProgress.toggle()
  100. return
  101. }
  102. func createProcessUploads() {
  103. if !self.dismiss {
  104. NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatas, completion: { _ in
  105. self.dismiss = true
  106. })
  107. }
  108. }
  109. if isUseAutoUploadFolder {
  110. DispatchQueue.global().async {
  111. let assets = self.assets.compactMap { $0.phAsset }
  112. let result = NCNetworking.shared.createFolder(assets: assets, selector: NCGlobal.shared.selectorUploadFile, useSubFolder: self.isUseAutoUploadSubFolder, account: self.userBaseUrl.account, urlBase: self.userBaseUrl.urlBase, userId: self.userBaseUrl.userId, withPush: false)
  113. DispatchQueue.main.async {
  114. self.showHUD = false
  115. self.uploadInProgress.toggle()
  116. if result {
  117. createProcessUploads()
  118. } else {
  119. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_error_createsubfolders_upload_")
  120. NCContentPresenter.shared.showError(error: error)
  121. }
  122. }
  123. }
  124. } else {
  125. createProcessUploads()
  126. }
  127. }
  128. }
  129. // MARK: - View
  130. @available(iOS 15, *)
  131. struct UploadAssetsView: View {
  132. @State private var fileName: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
  133. @State private var isMaintainOriginalFilename: Bool = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
  134. @State private var isAddFilenametype: Bool = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
  135. @State private var isPresentedSelect = false
  136. @State private var isPresentedUploadConflict = false
  137. @State private var isPresentedQuickLook = false
  138. @State private var isPresentedAlert = false
  139. @State private var fileNamePath = NSTemporaryDirectory() + "Photo.jpg"
  140. @State private var renameFileName: String = ""
  141. @State private var renameIndex: Int = 0
  142. @State private var metadata: tableMetadata?
  143. @State private var index: Int = 0
  144. var gridItems: [GridItem] = [GridItem()]
  145. @ObservedObject var uploadAssets: NCUploadAssets
  146. @Environment(\.presentationMode) var presentationMode
  147. init(uploadAssets: NCUploadAssets) {
  148. self.uploadAssets = uploadAssets
  149. uploadAssets.loadImages()
  150. }
  151. func getOriginalFilename() -> String {
  152. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  153. if let asset = uploadAssets.assets.first?.phAsset, let name = (asset.value(forKey: "filename") as? String) {
  154. return (name as NSString).deletingPathExtension
  155. } else {
  156. return ""
  157. }
  158. }
  159. func getTextServerUrl(_ serverUrl: String) -> String {
  160. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", uploadAssets.userBaseUrl.account, serverUrl)), let metadata = NCManageDatabase.shared.getMetadataFromOcId(directory.ocId) {
  161. return (metadata.fileNameView)
  162. } else {
  163. return (serverUrl as NSString).lastPathComponent
  164. }
  165. }
  166. func setFileNameMask(fileName: String?) -> String {
  167. guard let asset = uploadAssets.assets.first?.phAsset else { return "" }
  168. var preview: String = ""
  169. let creationDate = asset.creationDate ?? Date()
  170. CCUtility.setOriginalFileName(isMaintainOriginalFilename, key: NCGlobal.shared.keyFileNameOriginal)
  171. CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
  172. if let fileName = fileName {
  173. let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  174. if !fileName.isEmpty {
  175. CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
  176. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  177. fileDate: creationDate, fileType: asset.mediaType,
  178. keyFileName: NCGlobal.shared.keyFileNameMask,
  179. keyFileNameType: NCGlobal.shared.keyFileNameType,
  180. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  181. forcedNewFileName: false)
  182. } else {
  183. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  184. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  185. fileDate: creationDate,
  186. fileType: asset.mediaType,
  187. keyFileName: nil,
  188. keyFileNameType: NCGlobal.shared.keyFileNameType,
  189. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  190. forcedNewFileName: false)
  191. }
  192. } else {
  193. CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
  194. preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
  195. fileDate: creationDate,
  196. fileType: asset.mediaType,
  197. keyFileName: nil,
  198. keyFileNameType: NCGlobal.shared.keyFileNameType,
  199. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  200. forcedNewFileName: false)
  201. }
  202. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + (preview as NSString).deletingPathExtension
  203. }
  204. func save(completion: @escaping (_ metadatasNOConflict: [tableMetadata], _ metadatasUploadInConflict: [tableMetadata]) -> Void) {
  205. var metadatasNOConflict: [tableMetadata] = []
  206. var metadatasUploadInConflict: [tableMetadata] = []
  207. let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId, account: uploadAssets.userBaseUrl.account)
  208. var serverUrl = uploadAssets.isUseAutoUploadFolder ? autoUploadPath : uploadAssets.serverUrl
  209. let autoUploadSubfolderGranularity = NCManageDatabase.shared.getAccountAutoUploadSubfolderGranularity()
  210. for tlAsset in uploadAssets.assets {
  211. guard let asset = tlAsset.phAsset,
  212. let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }),
  213. let assetFileName = asset.value(forKey: "filename") as? NSString else { continue }
  214. var livePhoto: Bool = false
  215. let creationDate = asset.creationDate ?? Date()
  216. let ext = assetFileName.pathExtension.lowercased()
  217. let fileName = previewStore.fileName.isEmpty ?
  218. CCUtility.createFileName(assetFileName as String,
  219. fileDate: creationDate,
  220. fileType: asset.mediaType,
  221. keyFileName: NCGlobal.shared.keyFileNameMask,
  222. keyFileNameType: NCGlobal.shared.keyFileNameType,
  223. keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
  224. forcedNewFileName: false)!
  225. : (previewStore.fileName + "." + ext)
  226. if previewStore.assetType == .livePhoto && CCUtility.getLivePhoto() && previewStore.data == nil {
  227. livePhoto = true
  228. }
  229. // Auto upload with subfolder
  230. if uploadAssets.isUseAutoUploadSubFolder {
  231. let dateFormatter = DateFormatter()
  232. dateFormatter.dateFormat = "yyyy"
  233. let yearString = dateFormatter.string(from: creationDate)
  234. dateFormatter.dateFormat = "MM"
  235. let monthString = dateFormatter.string(from: creationDate)
  236. dateFormatter.dateFormat = "dd"
  237. let dayString = dateFormatter.string(from: creationDate)
  238. if (autoUploadSubfolderGranularity == 0) {
  239. serverUrl = autoUploadPath + "/" + yearString
  240. }
  241. else if (autoUploadSubfolderGranularity == 2) {
  242. serverUrl = autoUploadPath + "/" + yearString + "/" + monthString + "/" + dayString
  243. }
  244. else { // Month Granularity is default
  245. serverUrl = autoUploadPath + "/" + yearString + "/" + monthString
  246. }
  247. }
  248. // Check if is in upload
  249. let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", uploadAssets.userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
  250. if !isRecordInSessions.isEmpty { continue }
  251. 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)
  252. metadata.assetLocalIdentifier = asset.localIdentifier
  253. metadata.session = NCNetworking.shared.sessionIdentifierBackground
  254. metadata.sessionSelector = NCGlobal.shared.selectorUploadFile
  255. metadata.status = NCGlobal.shared.metadataStatusWaitUpload
  256. // Modified
  257. if let previewStore = uploadAssets.previewStore.first(where: { $0.id == asset.localIdentifier }), let data = previewStore.data {
  258. if metadata.contentType == "image/heic" {
  259. let fileNameNoExtension = (fileName as NSString).deletingPathExtension
  260. metadata.contentType = "image/jpeg"
  261. metadata.fileName = fileNameNoExtension + ".jpg"
  262. metadata.fileNameView = fileNameNoExtension + ".jpg"
  263. }
  264. let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(metadata.ocId, fileNameView: metadata.fileNameView)!
  265. do {
  266. try data.write(to: URL(fileURLWithPath: fileNamePath))
  267. metadata.isExtractFile = true
  268. metadata.size = NCUtilityFileSystem.shared.getFileSize(filePath: fileNamePath)
  269. metadata.creationDate = asset.creationDate as? NSDate ?? (Date() as NSDate)
  270. metadata.date = asset.modificationDate as? NSDate ?? (Date() as NSDate)
  271. } catch { }
  272. }
  273. if let result = NCManageDatabase.shared.getMetadataConflict(account: uploadAssets.userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
  274. metadata.fileName = result.fileName
  275. metadatasUploadInConflict.append(metadata)
  276. } else {
  277. metadatasNOConflict.append(metadata)
  278. }
  279. }
  280. completion(metadatasNOConflict, metadatasUploadInConflict)
  281. }
  282. func presentedQuickLook(index: Int) {
  283. var image: UIImage?
  284. if let imageData = uploadAssets.previewStore[index].data {
  285. image = UIImage(data: imageData)
  286. } else if let imageFullResolution = uploadAssets.previewStore[index].asset.fullResolutionImage?.fixedOrientation() {
  287. image = imageFullResolution
  288. }
  289. if let image = image {
  290. if let data = image.jpegData(compressionQuality: 1) {
  291. do {
  292. try data.write(to: URL(fileURLWithPath: fileNamePath))
  293. self.index = index
  294. isPresentedQuickLook = true
  295. } catch {
  296. }
  297. }
  298. }
  299. }
  300. func deleteAsset(index: Int) {
  301. uploadAssets.assets.remove(at: index)
  302. uploadAssets.previewStore.remove(at: index)
  303. if uploadAssets.previewStore.isEmpty {
  304. uploadAssets.dismiss = true
  305. }
  306. }
  307. var body: some View {
  308. NavigationView {
  309. ZStack(alignment: .top) {
  310. List {
  311. Section(footer: Text(NSLocalizedString("_modify_image_desc_", comment: ""))) {
  312. ScrollView(.horizontal) {
  313. LazyHGrid(rows: gridItems, alignment: .center, spacing: 10) {
  314. ForEach(0..<uploadAssets.previewStore.count, id: \.self) { index in
  315. let item = uploadAssets.previewStore[index]
  316. Menu {
  317. Button(action: {
  318. renameFileName = uploadAssets.previewStore[index].fileName
  319. renameIndex = index
  320. isPresentedAlert = true
  321. }) {
  322. Label(NSLocalizedString("_rename_", comment: ""), systemImage: "pencil")
  323. }
  324. if item.asset.type == .photo || item.asset.type == .livePhoto {
  325. Button(action: {
  326. presentedQuickLook(index: index)
  327. }) {
  328. Label(NSLocalizedString("_modify_", comment: ""), systemImage: "pencil.tip.crop.circle")
  329. }
  330. }
  331. if item.data != nil {
  332. Button(action: {
  333. if let image = uploadAssets.previewStore[index].asset.fullResolutionImage?.resizeImage(size: CGSize(width: 300, height: 300), isAspectRation: true) {
  334. uploadAssets.previewStore[index].image = image
  335. uploadAssets.previewStore[index].data = nil
  336. uploadAssets.previewStore[index].assetType = uploadAssets.previewStore[index].asset.type
  337. }
  338. }) {
  339. Label(NSLocalizedString("_undo_modify_", comment: ""), systemImage: "arrow.uturn.backward.circle")
  340. }
  341. }
  342. if item.data == nil && item.asset.type == .livePhoto && item.assetType == .livePhoto {
  343. Button(action: {
  344. uploadAssets.previewStore[index].assetType = .photo
  345. }) {
  346. Label(NSLocalizedString("_disable_livephoto_", comment: ""), systemImage: "livephoto.slash")
  347. }
  348. } else if item.data == nil && item.asset.type == .livePhoto && item.assetType == .photo {
  349. Button(action: {
  350. uploadAssets.previewStore[index].assetType = .livePhoto
  351. }) {
  352. Label(NSLocalizedString("_enable_livephoto_", comment: ""), systemImage: "livephoto")
  353. }
  354. }
  355. Button(role: .destructive, action: {
  356. deleteAsset(index: index)
  357. }) {
  358. Label(NSLocalizedString("_remove_", comment: ""), systemImage: "trash")
  359. }
  360. } label: {
  361. ImageAsset(uploadAssets: uploadAssets, index: index)
  362. .alert(NSLocalizedString("_rename_file_", comment: ""), isPresented: $isPresentedAlert) {
  363. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $renameFileName)
  364. .autocapitalization(.none)
  365. .autocorrectionDisabled()
  366. Button(NSLocalizedString("_rename_", comment: ""), action: {
  367. uploadAssets.previewStore[renameIndex].fileName = renameFileName.trimmingCharacters(in: .whitespacesAndNewlines)
  368. })
  369. Button(NSLocalizedString("_cancel_", comment: ""), role: .cancel, action: {})
  370. }
  371. }
  372. }
  373. }
  374. }
  375. }
  376. .redacted(reason: uploadAssets.previewStore.isEmpty ? .placeholder : [])
  377. Section {
  378. Toggle(isOn: $isMaintainOriginalFilename, label: {
  379. Text(NSLocalizedString("_maintain_original_filename_", comment: ""))
  380. .font(.system(size: 15))
  381. })
  382. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  383. if !isMaintainOriginalFilename {
  384. Toggle(isOn: $isAddFilenametype, label: {
  385. Text(NSLocalizedString("_add_filenametype_", comment: ""))
  386. .font(.system(size: 15))
  387. })
  388. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  389. }
  390. }
  391. Section {
  392. Toggle(isOn: $uploadAssets.isUseAutoUploadFolder, label: {
  393. Text(NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  394. .font(.system(size: 15))
  395. })
  396. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  397. if uploadAssets.isUseAutoUploadFolder {
  398. Toggle(isOn: $uploadAssets.isUseAutoUploadSubFolder, label: {
  399. Text(NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  400. .font(.system(size: 15))
  401. })
  402. .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
  403. }
  404. if !uploadAssets.isUseAutoUploadFolder {
  405. HStack {
  406. Label {
  407. if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == uploadAssets.serverUrl {
  408. Text("/")
  409. .font(.system(size: 15))
  410. .frame(maxWidth: .infinity, alignment: .trailing)
  411. } else {
  412. Text(self.getTextServerUrl(uploadAssets.serverUrl))
  413. .font(.system(size: 15))
  414. .frame(maxWidth: .infinity, alignment: .trailing)
  415. }
  416. } icon: {
  417. Image("folder")
  418. .renderingMode(.template)
  419. .resizable()
  420. .scaledToFit()
  421. .foregroundColor(Color(NCBrandColor.shared.brand))
  422. }
  423. }
  424. .contentShape(Rectangle())
  425. .onTapGesture {
  426. isPresentedSelect = true
  427. }
  428. }
  429. }
  430. Section {
  431. HStack {
  432. Text(NSLocalizedString("_filename_", comment: ""))
  433. if isMaintainOriginalFilename {
  434. Text(getOriginalFilename())
  435. .font(.system(size: 15))
  436. .frame(maxWidth: .infinity, alignment: .trailing)
  437. .foregroundColor(Color.gray)
  438. } else {
  439. TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
  440. .font(.system(size: 15))
  441. .modifier(TextFieldClearButton(text: $fileName))
  442. .multilineTextAlignment(.trailing)
  443. }
  444. }
  445. if !isMaintainOriginalFilename {
  446. Text(setFileNameMask(fileName: fileName))
  447. .font(.system(size: 11))
  448. .foregroundColor(Color.gray)
  449. }
  450. }
  451. .complexModifier { view in
  452. view.listRowSeparator(.hidden)
  453. }
  454. Button(NSLocalizedString("_save_", comment: "")) {
  455. if uploadAssets.isUseAutoUploadFolder, uploadAssets.isUseAutoUploadSubFolder {
  456. uploadAssets.showHUD = true
  457. }
  458. uploadAssets.uploadInProgress.toggle()
  459. save { metadatasNOConflict, metadatasUploadInConflict in
  460. if metadatasUploadInConflict.isEmpty {
  461. uploadAssets.dismissCreateFormUploadConflict(metadatas: metadatasNOConflict)
  462. } else {
  463. uploadAssets.metadatasNOConflict = metadatasNOConflict
  464. uploadAssets.metadatasUploadInConflict = metadatasUploadInConflict
  465. isPresentedUploadConflict = true
  466. }
  467. }
  468. }
  469. .frame(maxWidth: .infinity)
  470. .buttonStyle(ButtonRounded(disabled: uploadAssets.uploadInProgress))
  471. .listRowBackground(Color(UIColor.systemGroupedBackground))
  472. .disabled(uploadAssets.uploadInProgress)
  473. }
  474. .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
  475. .navigationBarTitleDisplayMode(.inline)
  476. HUDView(showHUD: $uploadAssets.showHUD, textLabel: NSLocalizedString("_wait_", comment: ""), image: "doc.badge.arrow.up")
  477. .offset(y: uploadAssets.showHUD ? 5 : -200)
  478. .animation(.easeOut)
  479. }
  480. }
  481. .navigationViewStyle(StackNavigationViewStyle())
  482. .sheet(isPresented: $isPresentedSelect) {
  483. SelectView(serverUrl: $uploadAssets.serverUrl)
  484. }
  485. .sheet(isPresented: $isPresentedUploadConflict) {
  486. UploadConflictView(delegate: uploadAssets, serverUrl: uploadAssets.serverUrl, metadatasUploadInConflict: uploadAssets.metadatasUploadInConflict, metadatasNOConflict: uploadAssets.metadatasNOConflict)
  487. }
  488. .fullScreenCover(isPresented: $isPresentedQuickLook) {
  489. ViewerQuickLook(url: URL(fileURLWithPath: fileNamePath), index: $index, isPresentedQuickLook: $isPresentedQuickLook, uploadAssets: uploadAssets)
  490. .ignoresSafeArea()
  491. }
  492. .onReceive(uploadAssets.$dismiss) { newValue in
  493. if newValue {
  494. presentationMode.wrappedValue.dismiss()
  495. }
  496. }
  497. .onTapGesture {
  498. UIApplication.shared.windows.filter { $0.isKeyWindow }.first?.endEditing(true)
  499. }
  500. .onDisappear {
  501. uploadAssets.dismiss = true
  502. }
  503. }
  504. struct ImageAsset: View {
  505. @ObservedObject var uploadAssets: NCUploadAssets
  506. @State var index: Int
  507. var body: some View {
  508. ZStack(alignment: .bottomTrailing) {
  509. if index < uploadAssets.previewStore.count {
  510. let item = uploadAssets.previewStore[index]
  511. Image(uiImage: item.image)
  512. .resizable()
  513. .aspectRatio(contentMode: .fill)
  514. .frame(width: 80, height: 80, alignment: .center)
  515. .cornerRadius(10)
  516. if item.assetType == .livePhoto && item.data == nil {
  517. Image(systemName: "livephoto")
  518. .resizable()
  519. .scaledToFit()
  520. .frame(width: 15, height: 15)
  521. .foregroundColor(.white)
  522. .padding(.horizontal, 5)
  523. .padding(.vertical, 5)
  524. } else if item.assetType == .video {
  525. Image(systemName: "video.fill")
  526. .resizable()
  527. .scaledToFit()
  528. .frame(width: 15, height: 15)
  529. .foregroundColor(.white)
  530. .padding(.horizontal, 5)
  531. .padding(.vertical, 5)
  532. }
  533. }
  534. }
  535. }
  536. }
  537. }
  538. // MARK: - Preview
  539. @available(iOS 15, *)
  540. struct UploadAssetsView_Previews: PreviewProvider {
  541. static var previews: some View {
  542. if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
  543. let uploadAssets = NCUploadAssets(assets: [], serverUrl: "/", userBaseUrl: appDelegate)
  544. UploadAssetsView(uploadAssets: uploadAssets)
  545. }
  546. }
  547. }