Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File is downloaded when opening text editor #2976

Merged
merged 4 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 35 additions & 10 deletions iOSClient/Data/NCManageDatabase+Metadata.swift
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,6 @@ extension tableMetadata {
return !isDirectoryE2EE && directory && size == 0 && e2eEncrypted && NCKeychain().isEndToEndEnabled(account: account)
}

var canOpenExternalEditor: Bool {
if isDocumentViewableOnly {
return false
}
let utility = NCUtility()
let editors = utility.isDirectEditing(account: account, contentType: contentType)
let isRichDocument = utility.isRichDocument(self)
return classFile == NKCommon.TypeClassFile.document.rawValue && editors.contains(NCGlobal.shared.editorText) && ((editors.contains(NCGlobal.shared.editorOnlyoffice) || isRichDocument))
}

var isWaitingTransfer: Bool {
status == NCGlobal.shared.metadataStatusWaitDownload || status == NCGlobal.shared.metadataStatusWaitUpload || status == NCGlobal.shared.metadataStatusUploadError
}
Expand Down Expand Up @@ -281,6 +271,41 @@ extension tableMetadata {
!isImage && !isAudioOrVideo && hasPreview && NCUtilityFileSystem().fileProviderStoragePreviewIconExists(ocId, etag: etag)
}

var isAvailableEditorView: Bool {
guard (classFile == NKCommon.TypeClassFile.document.rawValue) && NextcloudKit.shared.isNetworkReachable() else { return false }
let utility = NCUtility()
let directEditingEditors = utility.editorsDirectEditing(account: account, contentType: contentType)
let richDocumentEditor = utility.isTypeFileRichDocument(self)

if NCGlobal.shared.capabilityRichDocumentsEnabled && richDocumentEditor && directEditingEditors.isEmpty {
// RichDocument: Collabora
return true
} else if directEditingEditors.contains(NCGlobal.shared.editorText) || directEditingEditors.contains(NCGlobal.shared.editorOnlyoffice) {
// DirectEditing: Nextcloud Text - OnlyOffice
return true
}
return false
}

var isAvailableRichDocumentEditorView: Bool {
guard (classFile == NKCommon.TypeClassFile.document.rawValue) && NCGlobal.shared.capabilityRichDocumentsEnabled && NextcloudKit.shared.isNetworkReachable() else { return false }

if NCUtility().isTypeFileRichDocument(self) {
return true
}
return false
}

var isAvailableDirectEditingEditorView: Bool {
guard (classFile == NKCommon.TypeClassFile.document.rawValue) && NextcloudKit.shared.isNetworkReachable() else { return false }
let editors = NCUtility().editorsDirectEditing(account: account, contentType: contentType)

if editors.contains(NCGlobal.shared.editorText) || editors.contains(NCGlobal.shared.editorOnlyoffice) {
return true
}
return false
}

/// Returns false if the user is lokced out of the file. I.e. The file is locked but by somone else
func canUnlock(as user: String) -> Bool {
return !lock || (lockOwner == user && lockOwnerType == 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ extension NCCollectionViewCommon: UICollectionViewDelegate {
}
NCViewer().view(viewController: self, metadata: metadata, metadatas: metadatas, imageIcon: imageIcon)
return
}

if utilityFileSystem.fileProviderStorageExists(metadata) {
} else if metadata.isAvailableEditorView || utilityFileSystem.fileProviderStorageExists(metadata) {
NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon)
} else if NextcloudKit.shared.isNetworkReachable(),
let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
Expand Down
2 changes: 1 addition & 1 deletion iOSClient/Main/NCActionCenter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ class NCActionCenter: NSObject, UIDocumentInteractionControllerDelegate, NCSelec
case NCGlobal.shared.selectorLoadFileView:

guard UIApplication.shared.applicationState == .active else { return }
if metadata.contentType.contains("opendocument") && !self.utility.isRichDocument(metadata) {
if metadata.contentType.contains("opendocument") && !self.utility.isTypeFileRichDocument(metadata) {
self.openDocumentController(metadata: metadata, controller: controller)
} else if metadata.classFile == NKCommon.TypeClassFile.compress.rawValue || metadata.classFile == NKCommon.TypeClassFile.unknow.rawValue {
self.openDocumentController(metadata: metadata, controller: controller)
Expand Down
41 changes: 2 additions & 39 deletions iOSClient/Menu/NCCollectionViewCommon+Menu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,15 @@ extension NCCollectionViewCommon {
var actions = [NCMenuAction]()
let serverUrl = metadata.serverUrl + "/" + metadata.fileName
var isOffline: Bool = false
let applicationHandle = NCApplicationHandle()
var iconHeader: UIImage!

if metadata.directory, let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
isOffline = directory.offline
} else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
isOffline = localFile.offline
}

let editors = utility.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
let isRichDocument = utility.isRichDocument(metadata)
let applicationHandle = NCApplicationHandle()

var iconHeader: UIImage!

if imageIcon != nil {
iconHeader = imageIcon!
} else {
Expand Down Expand Up @@ -221,39 +217,6 @@ extension NCCollectionViewCommon {
}))
}

//
// OPEN with external editor
//
if metadata.canOpenExternalEditor {

var editor = ""
var title = ""
var icon: UIImage?

if editors.contains(NCGlobal.shared.editorOnlyoffice) {
editor = NCGlobal.shared.editorOnlyoffice
title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
icon = utility.loadImage(named: "onlyoffice", colors: [NCBrandColor.shared.iconImageColor])
} else if isRichDocument {
editor = NCGlobal.shared.editorCollabora
title = NSLocalizedString("_open_in_collabora_", comment: "")
icon = utility.loadImage(named: "collabora", colors: [NCBrandColor.shared.iconImageColor])
}

if !editor.isEmpty {
actions.append(
NCMenuAction(
title: title,
icon: icon!,
order: 70,
action: { _ in
NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
}
)
)
}
}

//
// SHARE
//
Expand Down
4 changes: 2 additions & 2 deletions iOSClient/Utility/NCUtility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class NCUtility: NSObject {
return path.contains("CoreSimulator")
}

func isRichDocument(_ metadata: tableMetadata) -> Bool {
func isTypeFileRichDocument(_ metadata: tableMetadata) -> Bool {
guard metadata.fileNameView != "." else { return false }
let fileExtension = (metadata.fileNameView as NSString).pathExtension
guard !fileExtension.isEmpty else { return false }
Expand All @@ -66,7 +66,7 @@ class NCUtility: NSObject {
return false
}

func isDirectEditing(account: String, contentType: String) -> [String] {
func editorsDirectEditing(account: String, contentType: String) -> [String] {
var editor: [String] = []
guard let results = NCManageDatabase.shared.getDirectEditingEditors(account: account) else { return editor }

Expand Down
85 changes: 36 additions & 49 deletions iOSClient/Viewer/NCViewer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,14 @@ import NextcloudKit
import QuickLook

class NCViewer: NSObject {

let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
let utilityFileSystem = NCUtilityFileSystem()
let utility = NCUtility()
private var viewerQuickLook: NCViewerQuickLook?
private var metadata = tableMetadata()
private var metadatas: [tableMetadata] = []

func view(viewController: UIViewController, metadata: tableMetadata, metadatas: [tableMetadata], imageIcon: UIImage?, editor: String = "", isRichDocument: Bool = false) {
var editor = editor
func view(viewController: UIViewController, metadata: tableMetadata, metadatas: [tableMetadata], imageIcon: UIImage?) {
self.metadata = metadata
self.metadatas = metadatas

Expand Down Expand Up @@ -95,11 +93,8 @@ class NCViewer: NSObject {
}
return
}
// EDITORS
let editors = utility.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
let availableRichDocument = utility.isRichDocument(metadata)
// RichDocument: Collabora
if (isRichDocument || (availableRichDocument && editors.isEmpty)) && NCGlobal.shared.capabilityRichDocumentsEnabled && NextcloudKit.shared.isNetworkReachable() {
if metadata.isAvailableRichDocumentEditorView {
if metadata.url.isEmpty {
NCActivityIndicator.shared.start(backgroundView: viewController.view)
NextcloudKit.shared.createUrlRichdocuments(fileID: metadata.fileId) { account, url, _, error in
Expand Down Expand Up @@ -128,55 +123,47 @@ class NCViewer: NSObject {
return
}
// DirectEditing: Nextcloud Text - OnlyOffice
if !editors.isEmpty, NextcloudKit.shared.isNetworkReachable() {
if editor.isEmpty {
if editors.contains(NCGlobal.shared.editorText) {
editor = NCGlobal.shared.editorText
} else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
editor = NCGlobal.shared.editorOnlyoffice
}
if metadata.isAvailableDirectEditingEditorView {
var options = NKRequestOptions()
var editor = ""
let editors = utility.editorsDirectEditing(account: metadata.account, contentType: metadata.contentType)
if editors.contains(NCGlobal.shared.editorText) {
editor = NCGlobal.shared.editorText
options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentNCText())
} else if editors.contains(NCGlobal.shared.editorOnlyoffice) {
editor = NCGlobal.shared.editorOnlyoffice
options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentOnlyOffice())
}
if editor == NCGlobal.shared.editorText || editor == NCGlobal.shared.editorOnlyoffice {
if metadata.url.isEmpty {
let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
var options = NKRequestOptions()
if editor == NCGlobal.shared.editorOnlyoffice {
options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentOnlyOffice())
} else {
options = NKRequestOptions(customUserAgent: utility.getCustomUserAgentNCText())
}
NCActivityIndicator.shared.start(backgroundView: viewController.view)
NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, options: options) { account, url, _, error in
NCActivityIndicator.shared.stop()
if error == .success && account == self.appDelegate.account && url != nil {
if let navigationController = viewController.navigationController,
let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
viewController.metadata = metadata
viewController.editor = editor
viewController.link = url!
viewController.imageIcon = imageIcon
navigationController.pushViewController(viewController, animated: true)
}
} else if error != .success {
NCContentPresenter().showError(error: error)
if metadata.url.isEmpty {
let fileNamePath = utilityFileSystem.getFileNamePath(metadata.fileName, serverUrl: metadata.serverUrl, urlBase: metadata.urlBase, userId: metadata.userId)
NCActivityIndicator.shared.start(backgroundView: viewController.view)
NextcloudKit.shared.NCTextOpenFile(fileNamePath: fileNamePath, editor: editor, options: options) { account, url, _, error in
NCActivityIndicator.shared.stop()
if error == .success && account == self.appDelegate.account && url != nil {
if let navigationController = viewController.navigationController,
let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
viewController.metadata = metadata
viewController.editor = editor
viewController.link = url!
viewController.imageIcon = imageIcon
navigationController.pushViewController(viewController, animated: true)
}
}
} else {
if let navigationController = viewController.navigationController,
let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
viewController.metadata = metadata
viewController.editor = editor
viewController.link = metadata.url
viewController.imageIcon = imageIcon
navigationController.pushViewController(viewController, animated: true)
} else if error != .success {
NCContentPresenter().showError(error: error)
}
}
} else {
let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_editor_unknown_")
NCContentPresenter().showError(error: error)
if let navigationController = viewController.navigationController,
let viewController: NCViewerNextcloudText = UIStoryboard(name: "NCViewerNextcloudText", bundle: nil).instantiateInitialViewController() as? NCViewerNextcloudText {
viewController.metadata = metadata
viewController.editor = editor
viewController.link = metadata.url
viewController.imageIcon = imageIcon
navigationController.pushViewController(viewController, animated: true)
}
}
return
}
return
}

// QLPreview
Expand Down
Loading