Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
martenrebane committed Jun 12, 2024
1 parent fea1659 commit f4d142d
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
12 changes: 5 additions & 7 deletions MoppApp/MoppApp/Extensions/URL+Additions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ extension URL {
func isValidPath() -> Bool {
return !self.pathComponents.contains("..")
}
}

class ValidPath {
static func getValidPath(url: URL) -> URL {
func getValidPath() -> URL? {
guard let appDirectoryURL = FileManager.default.urls(for: .applicationDirectory, in: .userDomainMask).first else {
return URL(string: "")!
return nil
}

guard let subdirectoryURLs = try? FileManager.default.contentsOfDirectory(at: appDirectoryURL, includingPropertiesForKeys: nil, options: .skipsHiddenFiles) else {
return URL(string: "")!
return nil
}

for subdirectoryURL in subdirectoryURLs {
let subdirectoryFilePath = FilePath(stringLiteral: subdirectoryURL.path)
if (FilePath(stringLiteral: url.path).starts(with: subdirectoryFilePath.lexicallyNormalized())) {
if (FilePath(stringLiteral: self.path).starts(with: subdirectoryFilePath.lexicallyNormalized())) {
return URL(fileURLWithPath: subdirectoryFilePath.description)
}
}
return URL(string: "")!
return nil
}
}
10 changes: 5 additions & 5 deletions MoppApp/MoppApp/MoppApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -323,11 +323,11 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate {
pathExtension = MimeTypeExtractor.determineContainer(mimetype: MimeTypeExtractor.getMimeTypeFromContainer(filePath: newUrl), fileExtension: newUrl.pathExtension)

do {
guard newUrl.isValidPath() else {
guard let validUrl = newUrl.getValidPath(), validUrl.isValidPath() else {
printLog("Unable to open file. Invalid path: \(newUrl.path)")
return false
}
let newData: Data? = try Data(contentsOf: ValidPath.getValidPath(url: newUrl))
let newData: Data? = try Data(contentsOf: validUrl)
let fileName: String = newUrl.deletingPathExtension().lastPathComponent.sanitize()
let tempDirectoryPath: URL? = MoppFileManager.shared.tempCacheDirectoryPath()
guard let tempDirectory = tempDirectoryPath else {
Expand All @@ -343,11 +343,11 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate {
return false
}
do {
guard filePath.isValidPath() else {
guard let validUrl = filePath.getValidPath(), validUrl.isValidPath() else {
printLog("Unable to write to file. Invalid path: \(filePath.path)")
return false
}
try newUrlData.write(to: ValidPath.getValidPath(url: filePath), options: .atomic)
try newUrlData.write(to: validUrl, options: .atomic)
newUrl = filePath
if !isFileEmpty {
fileUrls.append(newUrl)
Expand Down Expand Up @@ -375,7 +375,7 @@ class MoppApp: UIApplication, URLSessionDelegate, URLSessionDownloadDelegate {
newUrl.deletePathExtension()
newUrl.appendPathExtension(ContainerFormatCdoc)
}
guard newUrl.isValidPath() else {
guard let validUrl = newUrl.getValidPath(), validUrl.isValidPath() else {
printLog("Unable to move file. Invalid path: \(newUrl.path)")
return false
}
Expand Down

0 comments on commit f4d142d

Please sign in to comment.