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

Filename validator #2984

Merged
merged 24 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
2 changes: 1 addition & 1 deletion Brand/NCBrand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ let userAgent: String = {
var disable_crash_service: Bool = false
var disable_log: Bool = false
var disable_mobileconfig: Bool = false
var disable_show_more_nextcloud_apps_in_settings: Bool = false
var disable_show_more_nextcloud_apps_in_settings: Bool = true
var doNotAskPasscodeAtStartup: Bool = false

// Internal option behaviour
Expand Down
4 changes: 2 additions & 2 deletions File Provider Extension/FileProviderData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class fileProviderData: NSObject {
homeServerUrl = utilityFileSystem.getHomeServer(urlBase: activeAccount.urlBase, userId: activeAccount.userId)

NCManageDatabase.shared.setCapabilities(account: account)
NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup, delegate: NCNetworking.shared)
NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, delegate: NCNetworking.shared)
NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate

return tableAccount.init(value: activeAccount)
Expand All @@ -114,7 +114,7 @@ class fileProviderData: NSObject {

NCManageDatabase.shared.setCapabilities(account: account)

NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, groupIdentifier: NCBrandOptions.shared.capabilitiesGroup, delegate: NCNetworking.shared)
NextcloudKit.shared.setup(account: activeAccount.account, user: activeAccount.user, userId: activeAccount.userId, password: NCKeychain().getPassword(account: activeAccount.account), urlBase: activeAccount.urlBase, userAgent: userAgent, nextcloudVersion: NCGlobal.shared.capabilityServerVersionMajor, delegate: NCNetworking.shared)
NCNetworking.shared.delegate = providerExtension as? NCNetworkingDelegate

return tableAccount.init(value: activeAccount)
Expand Down
8 changes: 6 additions & 2 deletions File Provider Extension/FileProviderExtension+Actions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extension FileProviderExtension {
}
}
} else {
completionHandler(nil, NSFileProviderError(.serverUnreachable))
completionHandler(nil, NSFileProviderError(.filenameCollision))
}
}
}
Expand All @@ -70,7 +70,7 @@ extension FileProviderExtension {
let fileName = metadata.fileName

NextcloudKit.shared.deleteFileOrFolder(serverUrlFileName: serverUrlFileName, account: metadata.account) { account, error in
if error == .success { // || error == kOCErrorServerPathNotFound {
if error == .success {
let fileNamePath = self.utilityFileSystem.getDirectoryProviderStorageOcId(itemIdentifier.rawValue)

do {
Expand Down Expand Up @@ -122,6 +122,8 @@ extension FileProviderExtension {
let item = FileProviderItem(metadata: metadata, parentItemIdentifier: parentItemIdentifier)

completionHandler(item, nil)
} else if error.errorCode == NCGlobal.shared.errorBadRequest {
completionHandler(nil, NSFileProviderError(.noSuchItem, userInfo: [NSLocalizedDescriptionKey: error.errorDescription, NSLocalizedFailureReasonErrorKey: ""]))
} else {
completionHandler(nil, NSFileProviderError(.serverUnreachable))
}
Expand Down Expand Up @@ -161,6 +163,8 @@ extension FileProviderExtension {
}
let item = FileProviderItem(metadata: tableMetadata.init(value: metadata), parentItemIdentifier: parentItemIdentifier)
completionHandler(item, nil)
} else if error.errorCode == NCGlobal.shared.errorBadRequest {
completionHandler(nil, NSFileProviderError(.noSuchItem, userInfo: [NSLocalizedDescriptionKey: error.errorDescription, NSLocalizedFailureReasonErrorKey: ""]))
} else {
completionHandler(nil, NSFileProviderError(.serverUnreachable))
}
Expand Down
24 changes: 2 additions & 22 deletions Nextcloud.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@
F70CAE3A1F8CF31A008125FD /* NCEndToEndEncryption.m in Sources */ = {isa = PBXBuildFile; fileRef = F70CAE391F8CF31A008125FD /* NCEndToEndEncryption.m */; };
F70CEF5623E9C7E50007035B /* UIColor+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70CEF5523E9C7E50007035B /* UIColor+Extension.swift */; };
F70D7C3725FFBF82002B9E34 /* NCCollectionViewCommon.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D7C3525FFBF81002B9E34 /* NCCollectionViewCommon.swift */; };
F70D87CF25EE6E58008CBBBD /* NCRenameFile.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F70D87CD25EE6E58008CBBBD /* NCRenameFile.storyboard */; };
F70D87D025EE6E58008CBBBD /* NCRenameFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D87CE25EE6E58008CBBBD /* NCRenameFile.swift */; };
F70D8D8124A4A9BF000A5756 /* NCNetworkingProcess.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D8D8024A4A9BF000A5756 /* NCNetworkingProcess.swift */; };
F710D1F52405770F00A6033D /* NCViewerPDF.swift in Sources */ = {isa = PBXBuildFile; fileRef = F710D1F42405770F00A6033D /* NCViewerPDF.swift */; };
F710D2022405826100A6033D /* NCViewer+Menu.swift in Sources */ = {isa = PBXBuildFile; fileRef = F710D2012405826100A6033D /* NCViewer+Menu.swift */; };
Expand Down Expand Up @@ -712,8 +710,6 @@
F79B646226CA661600838ACA /* UIControl+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79B645F26CA661600838ACA /* UIControl+Extension.swift */; };
F79B646326CA661600838ACA /* UIControl+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79B645F26CA661600838ACA /* UIControl+Extension.swift */; };
F79B869B265E19D40085C0E0 /* NSMutableAttributedString+Extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79B869A265E19D40085C0E0 /* NSMutableAttributedString+Extension.swift */; };
F79EC77F26316193004E59D6 /* NCRenameFile.swift in Sources */ = {isa = PBXBuildFile; fileRef = F70D87CE25EE6E58008CBBBD /* NCRenameFile.swift */; };
F79EC784263161BA004E59D6 /* NCRenameFile.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F70D87CD25EE6E58008CBBBD /* NCRenameFile.storyboard */; };
F79EC78926316AC4004E59D6 /* NCPopupViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = F702F30725EE5D47008F8E80 /* NCPopupViewController.swift */; };
F79EDAA326B004980007D134 /* NCPlayerToolBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79EDA9F26B004980007D134 /* NCPlayerToolBar.swift */; };
F79EDAA526B004980007D134 /* NCPlayer.swift in Sources */ = {isa = PBXBuildFile; fileRef = F79EDAA126B004980007D134 /* NCPlayer.swift */; };
Expand Down Expand Up @@ -1217,8 +1213,6 @@
F70CAE391F8CF31A008125FD /* NCEndToEndEncryption.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = NCEndToEndEncryption.m; sourceTree = "<group>"; };
F70CEF5523E9C7E50007035B /* UIColor+Extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColor+Extension.swift"; sourceTree = "<group>"; };
F70D7C3525FFBF81002B9E34 /* NCCollectionViewCommon.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCCollectionViewCommon.swift; sourceTree = "<group>"; };
F70D87CD25EE6E58008CBBBD /* NCRenameFile.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = NCRenameFile.storyboard; sourceTree = "<group>"; };
F70D87CE25EE6E58008CBBBD /* NCRenameFile.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = NCRenameFile.swift; sourceTree = "<group>"; };
F70D8D8024A4A9BF000A5756 /* NCNetworkingProcess.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NCNetworkingProcess.swift; sourceTree = "<group>"; };
F70F2BA4225F2D8900EBB73E /* ZIPFoundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = ZIPFoundation.framework; path = Carthage/Build/iOS/ZIPFoundation.framework; sourceTree = "<group>"; };
F70F96AF2874394B006C8379 /* Nextcloud-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "Nextcloud-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2082,15 +2076,6 @@
path = Color;
sourceTree = "<group>";
};
F70D87CC25EE6E58008CBBBD /* Rename file */ = {
isa = PBXGroup;
children = (
F70D87CD25EE6E58008CBBBD /* NCRenameFile.storyboard */,
F70D87CE25EE6E58008CBBBD /* NCRenameFile.swift */,
);
path = "Rename file";
sourceTree = "<group>";
};
F713418B2597513800768D21 /* PushNotification */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -2980,7 +2965,6 @@
F7381ED9218218A4000B1560 /* Offline */,
F713418B2597513800768D21 /* PushNotification */,
F765F72E25237E3F00391DBE /* Recent */,
F70D87CC25EE6E58008CBBBD /* Rename file */,
F7CADB3D23CCDDA1000EEC78 /* RichWorkspace */,
F76882042C0DD1E7001CF441 /* Settings */,
F758B41E212C516300515F55 /* Scan document */,
Expand Down Expand Up @@ -3668,7 +3652,6 @@
F7145A231D12E3B700CAFEEC /* Localizable.strings in Resources */,
F746EC51273906C40052598D /* NCViewCertificateDetails.storyboard in Resources */,
F76C26A62850D3A500E42BDF /* Images.xcassets in Resources */,
F79EC784263161BA004E59D6 /* NCRenameFile.storyboard in Resources */,
F714805E262ED52900693E51 /* NCSectionFooter.xib in Resources */,
F700222D1EC479840080073F /* Custom.xcassets in Resources */,
);
Expand Down Expand Up @@ -3714,7 +3697,6 @@
F702F2F125EE5CDB008F8E80 /* NCLogin.storyboard in Resources */,
F723985C253C95CE00257F49 /* NCViewerRichdocument.storyboard in Resources */,
F758B45A212C564000515F55 /* NCScan.storyboard in Resources */,
F70D87CF25EE6E58008CBBBD /* NCRenameFile.storyboard in Resources */,
F765F73225237E3F00391DBE /* NCRecent.storyboard in Resources */,
F78F74342163757000C2ADAD /* NCTrash.storyboard in Resources */,
F702F30225EE5D2C008F8E80 /* english.txt in Resources */,
Expand Down Expand Up @@ -4046,7 +4028,6 @@
F76D364728A4F8BF00214537 /* NCActivityIndicator.swift in Sources */,
F73EF7CA2B0225610087E6E9 /* NCManageDatabase+PhotoLibrary.swift in Sources */,
F749B654297B0F2400087535 /* NCManageDatabase+Avatar.swift in Sources */,
F79EC77F26316193004E59D6 /* NCRenameFile.swift in Sources */,
AF22B208277B4E4C00DAB0CC /* NCCreateFormUploadConflictCell.swift in Sources */,
F74C86382AEFBE64009A1D4A /* NCImageCache.swift in Sources */,
F73EF7C22B02250B0087E6E9 /* NCManageDatabase+GPS.swift in Sources */,
Expand Down Expand Up @@ -4234,7 +4215,6 @@
F702F30825EE5D47008F8E80 /* NCPopupViewController.swift in Sources */,
F733598125C1C188002ABA72 /* NCAskAuthorization.swift in Sources */,
370D26AF248A3D7A00121797 /* NCCellProtocol.swift in Sources */,
F70D87D025EE6E58008CBBBD /* NCRenameFile.swift in Sources */,
F768822C2C0DD1E7001CF441 /* NCKeychain.swift in Sources */,
F71CD6CA2930D7B1006C95C1 /* NCApplicationHandle.swift in Sources */,
F73EF7D72B0226080087E6E9 /* NCManageDatabase+Tip.swift in Sources */,
Expand Down Expand Up @@ -5778,8 +5758,8 @@
isa = XCRemoteSwiftPackageReference;
repositoryURL = "https://github.com/nextcloud/NextcloudKit";
requirement = {
kind = exactVersion;
version = 4.0.6;
branch = "filename-validator";
kind = branch;
};
};
F788ECC5263AAAF900ADC67F /* XCRemoteSwiftPackageReference "MarkdownKit" */ = {
Expand Down
11 changes: 8 additions & 3 deletions Share/NCShareExtension+DataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ extension NCShareExtension: UICollectionViewDelegate {
showAlert(title: "_info_", description: "_e2e_goto_settings_for_enable_")
}

if let fileNameError = FileNameValidator.shared.checkFileName(metadata.fileNameView) {
present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)
return
}

self.serverUrl = serverUrl
reloadDatasource(withLoadFolder: true)
setNavigationBar(navigationTitle: metadata.fileNameView)
Expand Down Expand Up @@ -84,7 +89,7 @@ extension NCShareExtension: UICollectionViewDataSource {
return UICollectionViewCell()
}

cell.listCellDelegate = self
// cell.listCellDelegate = self

cell.fileObjectId = metadata.ocId
cell.indexPath = indexPath
Expand Down Expand Up @@ -176,7 +181,7 @@ extension NCShareExtension: UITableViewDelegate {
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
guard !uploadStarted else { return }
let fileName = filesName[indexPath.row]
renameFile(named: fileName)
// renameFile(named: fileName)
}
}

Expand All @@ -190,7 +195,7 @@ extension NCShareExtension: UITableViewDataSource {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as? NCShareCell else { return UITableViewCell() }
let fileName = filesName[indexPath.row]
cell.setup(fileName: fileName)
cell.delegate = self
// cell.delegate = self
return cell
}
}
1 change: 1 addition & 0 deletions Share/NCShareExtension+Files.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import Foundation
import UniformTypeIdentifiers
import NextcloudKit

extension NCShareExtension {
@objc func reloadDatasource(withLoadFolder: Bool) {
Expand Down
58 changes: 6 additions & 52 deletions Share/NCShareExtension+NCDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,59 +93,13 @@ extension NCShareExtension: NCAccountRequestDelegate {

reloadDatasource(withLoadFolder: true)
setNavigationBar(navigationTitle: NCBrandOptions.shared.brand)
}
}

extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCellDelegate {
func removeFile(named fileName: String) {
guard let index = self.filesName.firstIndex(of: fileName) else {
return showAlert(title: "_file_not_found_", description: fileName)
}
self.filesName.remove(at: index)
if self.filesName.isEmpty {
cancel(with: NCShareExtensionError.noFiles)
} else {
self.setCommandView()
}
}

func renameFile(named fileName: String) {
guard let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile else { return }

let resultInternalType = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false)
vcRename.delegate = self
vcRename.fileName = fileName
vcRename.indexPath = IndexPath()
if let previewImage = UIImage.downsample(imageAt: URL(fileURLWithPath: NSTemporaryDirectory() + fileName), to: CGSize(width: 140, height: 140)) {
vcRename.imagePreview = previewImage
} else {
vcRename.imagePreview = UIImage(named: resultInternalType.iconName) ?? NCImageCache.images.file
}

let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)

self.present(popup, animated: true)
}

func rename(fileName: String, fileNameNew: String) {
guard fileName != fileNameNew else { return }
guard let fileIx = self.filesName.firstIndex(of: fileName),
!self.filesName.contains(fileNameNew),
utilityFileSystem.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew)) else {
return showAlert(title: "_single_file_conflict_title_", description: "'\(fileName)' -> '\(fileNameNew)'")
}

filesName[fileIx] = fileNameNew
tableView.reloadData()
}

func tapShareListItem(with objectId: String, indexPath: IndexPath, sender: Any) {
}

func tapMoreListItem(with objectId: String, namedButtonMore: String, image: UIImage?, indexPath: IndexPath, sender: Any) {
}

func longPressListItem(with objectId: String, indexPath: IndexPath, gestureRecognizer: UILongPressGestureRecognizer) {
FileNameValidator.shared.setup(
forbiddenFileNames: NCGlobal.shared.capabilityForbiddenFileNames,
forbiddenFileNameBasenames: NCGlobal.shared.capabilityForbiddenFileNameBasenames,
forbiddenFileNameCharacters: NCGlobal.shared.capabilityForbiddenFileNameCharacters,
forbiddenFileNameExtensions: NCGlobal.shared.capabilityForbiddenFileNameExtensions
)
}
}

Expand Down
7 changes: 7 additions & 0 deletions Share/NCShareExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,12 @@ extension NCShareExtension {

var conflicts: [tableMetadata] = []
for fileName in filesName {
if let fileNameError = FileNameValidator.shared.checkFileName(fileName) {
present(UIAlertController.warning(message: "\(fileNameError.errorDescription) \(NSLocalizedString("_please_rename_file_", comment: ""))"), animated: true)

continue
}

let ocId = NSUUID().uuidString
let toPath = utilityFileSystem.getDirectoryProviderStorageOcId(ocId, fileNameView: fileName)
guard utilityFileSystem.copyFile(atPath: (NSTemporaryDirectory() + fileName), toPath: toPath) else { continue }
Expand Down Expand Up @@ -335,6 +341,7 @@ extension NCShareExtension {
guard uploadStarted else { return }
guard uploadMetadata.count > counterUploaded else { return DispatchQueue.main.async { self.finishedUploading() } }
let metadata = uploadMetadata[counterUploaded]

let results = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: metadata.fileNameView, mimeType: metadata.contentType, directory: false)
metadata.contentType = results.mimeType
metadata.iconName = results.iconName
Expand Down
Loading
Loading