123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- //
- // NCUploadAssets.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 04/01/23.
- // Copyright © 2023 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import SwiftUI
- import NextcloudKit
- class NCHostingUploadAssetsView: NSObject {
- @objc func makeShipDetailsUI(assets: [PHAsset], cryptated: Bool, session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) -> UIViewController {
- let uploadAssets = NCUploadAssets(assets: assets, session: session, userBaseUrl: userBaseUrl, serverUrl: serverUrl)
- let details = UploadAssetsView(uploadAssets: uploadAssets, serverUrl: serverUrl)
- let vc = UIHostingController(rootView: details)
- return vc
- }
- }
- class NCUploadAssets: ObservableObject {
- internal var assets: [PHAsset]
- internal var session: String
- internal var userBaseUrl: NCUserBaseUrl
- @Published var serverUrl: String
- @Published var isMaintainOriginalFilename: Bool = CCUtility.getOriginalFileName(NCGlobal.shared.keyFileNameOriginal)
- @Published var isAddFilenametype: Bool = CCUtility.getFileNameType(NCGlobal.shared.keyFileNameType)
- init(assets: [PHAsset], session: String, userBaseUrl: NCUserBaseUrl, serverUrl: String) {
- self.assets = assets
- self.session = session
- self.userBaseUrl = userBaseUrl
- self.serverUrl = serverUrl
- }
- func setFileNameMask(fileName: String?) -> String {
- guard let asset = assets.first else { return "" }
- var preview: String = ""
- let creationDate = asset.creationDate ?? Date()
- CCUtility.setFileNameType(isAddFilenametype, key: NCGlobal.shared.keyFileNameType)
- if let fileName = fileName {
- let fileName = fileName.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
- if !fileName.isEmpty {
- CCUtility.setFileNameMask(fileName, key: NCGlobal.shared.keyFileNameMask)
- preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
- fileDate: creationDate, fileType: asset.mediaType,
- keyFileName: NCGlobal.shared.keyFileNameMask,
- keyFileNameType: NCGlobal.shared.keyFileNameType,
- keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
- forcedNewFileName: false)
- } else {
- CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
- preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
- fileDate: creationDate,
- fileType: asset.mediaType,
- keyFileName: nil,
- keyFileNameType: NCGlobal.shared.keyFileNameType,
- keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
- forcedNewFileName: false)
- }
- } else {
- CCUtility.setFileNameMask("", key: NCGlobal.shared.keyFileNameMask)
- preview = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
- fileDate: creationDate,
- fileType: asset.mediaType,
- keyFileName: nil,
- keyFileNameType: NCGlobal.shared.keyFileNameType,
- keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
- forcedNewFileName: false)
- }
- return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM, MMM, DD, YY, YYYY, HH, hh, mm, ss, ampm") + ":" + "\n\n" + preview
- }
- func getOriginalFilename() -> String {
- if let asset = assets.first, let name = (asset.value(forKey: "filename") as? String) {
- return name
- } else {
- return ""
- }
- }
- func save(completion: @escaping (_ openConflictViewController: Bool) -> Void) {
- var metadatasNOConflict: [tableMetadata] = []
- var metadatasUploadInConflict: [tableMetadata] = []
- let autoUploadPath = NCManageDatabase.shared.getAccountAutoUploadPath(urlBase: userBaseUrl.urlBase, userId: userBaseUrl.userId, account: userBaseUrl.account)
- for asset in self.assets {
- var serverUrl = self.serverUrl
- var livePhoto: Bool = false
- let creationDate = asset.creationDate ?? Date()
- let fileName = CCUtility.createFileName(asset.value(forKey: "filename") as? String,
- fileDate: creationDate,
- fileType: asset.mediaType,
- keyFileName: NCGlobal.shared.keyFileNameMask,
- keyFileNameType: NCGlobal.shared.keyFileNameType,
- keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginal,
- forcedNewFileName: false)!
- if asset.mediaSubtypes.contains(.photoLive) && CCUtility.getLivePhoto() {
- livePhoto = true
- }
- // Check if is in upload
- let isRecordInSessions = NCManageDatabase.shared.getAdvancedMetadatas(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileName == %@ AND session != ''", userBaseUrl.account, serverUrl, fileName), sorted: "fileName", ascending: false)
- if !isRecordInSessions.isEmpty { continue }
- let metadataForUpload = NCManageDatabase.shared.createMetadata(account: userBaseUrl.account, user: userBaseUrl.user, userId: userBaseUrl.userId, fileName: fileName, fileNameView: fileName, ocId: NSUUID().uuidString, serverUrl: serverUrl, urlBase: userBaseUrl.urlBase, url: "", contentType: "", isLivePhoto: livePhoto)
- metadataForUpload.assetLocalIdentifier = asset.localIdentifier
- metadataForUpload.session = self.session
- metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
- metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
- if let result = NCManageDatabase.shared.getMetadataConflict(account: userBaseUrl.account, serverUrl: serverUrl, fileNameView: fileName) {
- metadataForUpload.fileName = result.fileName
- metadatasUploadInConflict.append(metadataForUpload)
- } else {
- metadatasNOConflict.append(metadataForUpload)
- }
- }
- // Verify if file(s) exists
- if !metadatasUploadInConflict.isEmpty {
- /*
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.4) {
- if let conflict = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict {
- conflict.serverUrl = self.serverUrl
- conflict.metadatasNOConflict = metadatasNOConflict
- conflict.metadatasUploadInConflict = metadatasUploadInConflict
- conflict.delegate = self.appDelegate
- self.appDelegate.window?.rootViewController?.present(conflict, animated: true, completion: nil)
- }
- }
- */
- completion(true)
- } else {
- NCNetworkingProcessUpload.shared.createProcessUploads(metadatas: metadatasNOConflict, completion: { _ in })
- completion(false)
- }
- }
- }
- // MARK: - Delegate
- extension NCUploadAssets: NCSelectDelegate {
- func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, items: [Any], overwrite: Bool, copy: Bool, move: Bool) {
- if let serverUrl = serverUrl {
- CCUtility.setDirectoryScanDocument(serverUrl)
- self.serverUrl = serverUrl
- }
- }
- }
- extension NCUploadAssets: NCCreateFormUploadConflictDelegate {
- func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
- /*
- if let metadata = metadatas?.first {
- self.showHUD.toggle()
- createPDF(metadata: metadata) { error in
- if !error {
- self.showHUD.toggle()
- NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDismissScanDocument)
- }
- }
- }
- */
- }
- }
- // MARK: - View
- struct UploadAssetsView: View {
- @State private var serverUrl: String
- @State var fileName: String = CCUtility.getFileNameMask(NCGlobal.shared.keyFileNameMask)
- @State var isPresentedSelect = false
- @State var isPresentedUploadConflict = false
- @ObservedObject var uploadAssets: NCUploadAssets
- init(uploadAssets: NCUploadAssets, serverUrl: String) {
- self.uploadAssets = uploadAssets
- self.serverUrl = serverUrl
- }
- var body: some View {
- NavigationView {
- List {
- Section(header: Text(NSLocalizedString("_save_path_", comment: ""))) {
- HStack {
- Label {
- if NCUtilityFileSystem.shared.getHomeServer(urlBase: uploadAssets.userBaseUrl.urlBase, userId: uploadAssets.userBaseUrl.userId) == serverUrl {
- Text("/")
- .frame(maxWidth: .infinity, alignment: .trailing)
- } else {
- Text((serverUrl as NSString).lastPathComponent)
- .frame(maxWidth: .infinity, alignment: .trailing)
- }
- } icon: {
- Image("folder")
- .renderingMode(.template)
- .resizable()
- .scaledToFit()
- .foregroundColor(Color(NCBrandColor.shared.brand))
- }
- }
- .contentShape(Rectangle())
- .onTapGesture {
- isPresentedSelect = true
- }
- }
- Section(header: Text(NSLocalizedString("_mode_filename_", comment: ""))) {
- Toggle(NSLocalizedString("_maintain_original_filename_", comment: ""), isOn: $uploadAssets.isMaintainOriginalFilename)
- .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
- .onChange(of: uploadAssets.isMaintainOriginalFilename) { _ in }
- Toggle(NSLocalizedString("_add_filenametype_", comment: ""), isOn: $uploadAssets.isAddFilenametype)
- .toggleStyle(SwitchToggleStyle(tint: Color(NCBrandColor.shared.brand)))
- .onChange(of: uploadAssets.isAddFilenametype) { _ in }
- }
- Section(header: Text(NSLocalizedString("_filename_", comment: ""))) {
- HStack {
- Text(NSLocalizedString("_filename_", comment: ""))
- if uploadAssets.isMaintainOriginalFilename {
- Text(uploadAssets.getOriginalFilename())
- .frame(maxWidth: .infinity, alignment: .trailing)
- } else {
- TextField(NSLocalizedString("_enter_filename_", comment: ""), text: $fileName)
- .modifier(TextFieldClearButton(text: $fileName))
- .multilineTextAlignment(.trailing)
- }
- }
- if !uploadAssets.isMaintainOriginalFilename {
- Text(uploadAssets.setFileNameMask(fileName: fileName))
- }
- }
- .complexModifier { view in
- if #available(iOS 15, *) {
- view.listRowSeparator(.hidden)
- }
- }
- Button(NSLocalizedString("_save_", comment: "")) {
- }
- .frame(maxWidth: .infinity)
- .buttonStyle(ButtonRounded(disabled: false))
- .listRowBackground(Color(UIColor.systemGroupedBackground))
- }
- .navigationTitle(NSLocalizedString("_upload_photos_videos_", comment: ""))
- .navigationBarTitleDisplayMode(.inline)
- }
- .sheet(isPresented: $isPresentedSelect) {
- NCSelectView(serverUrl: $serverUrl)
- }
- .sheet(isPresented: $isPresentedUploadConflict) {
- if let serverUrl = serverUrl {
- NCUploadConflictRepresentedView(delegate: uploadAssets, serverUrl: serverUrl, metadatasUploadInConflict: []) // uploadAssets.metadata
- }
- }
- }
- }
- // MARK: - Preview
- struct UploadAssetsView_Previews: PreviewProvider {
- static var previews: some View {
- if let appDelegate = UIApplication.shared.delegate as? AppDelegate {
- let uploadAssets = NCUploadAssets(assets: [], session: "", userBaseUrl: appDelegate, serverUrl: "/")
- UploadAssetsView(uploadAssets: uploadAssets, serverUrl: "/")
- }
- }
- }
|