Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
speedyfriend433 authored Jul 5, 2024
1 parent 85d0c2a commit 95cba46
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 69 deletions.
3 changes: 1 addition & 2 deletions DirectoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
// Created by Speedyfriend67 on 27.06.24
//
//

import SwiftUI

#if canImport(UIKit)
Expand Down Expand Up @@ -252,4 +251,4 @@ struct DirectoryView: View {
return nil
}
}
}
}
82 changes: 18 additions & 64 deletions FileManagerViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class FileManagerViewModel: ObservableObject {
DispatchQueue.global(qos: .userInitiated).async {
do {
let directoryContents = try self.fileManager.contentsOfDirectory(at: self.directory, includingPropertiesForKeys: [.isDirectoryKey, .fileSizeKey, .creationDateKey, .contentModificationDateKey, .isSymbolicLinkKey], options: [])
var fileSystemItems: [FileSystemItem] = []
for url in directoryContents {
let resourceValues = try? url.resourceValues(forKeys: [.isDirectoryKey, .fileSizeKey, .creationDateKey, .contentModificationDateKey, .isSymbolicLinkKey])
let isDirectory = resourceValues?.isDirectory ?? false
Expand All @@ -69,11 +70,10 @@ class FileManagerViewModel: ObservableObject {
let creationDate = resourceValues?.creationDate ?? Date()
let modificationDate = resourceValues?.contentModificationDate ?? Date()
let fileSystemItem = FileSystemItem(name: url.lastPathComponent, isDirectory: isDirectory, url: url, size: fileSize, creationDate: creationDate, modificationDate: modificationDate, isSymlink: isSymlink)
DispatchQueue.main.async {
self.items.append(fileSystemItem)
}
fileSystemItems.append(fileSystemItem)
}
DispatchQueue.main.async {
self.items = fileSystemItems
self.sortItems()
self.filterItems()
self.isSearching = false
Expand All @@ -91,23 +91,27 @@ class FileManagerViewModel: ObservableObject {
selectedFile = item
}

func getFileMetadata(at url: URL) -> (name: String, size: String) {
var size = "Unknown"
let name = url.lastPathComponent
func getFileMetadata(at url: URL) -> [String: String] {
var metadata: [String: String] = [:]
metadata["Name"] = url.lastPathComponent
metadata["Size"] = getFileSize(at: url)
metadata["Creation Date"] = getCreationDate(at: url)
metadata["Modification Date"] = getModificationDate(at: url)
return metadata
}

func getFileSize(at url: URL) -> String {
do {
let attributes = try fileManager.attributesOfItem(atPath: url.path)
if let fileSize = attributes[.size] as? NSNumber {
size = ByteCountFormatter.string(fromByteCount: fileSize.int64Value, countStyle: .file)
if let size = attributes[.size] as? NSNumber {
return ByteCountFormatter.string(fromByteCount: size.int64Value, countStyle: .file)
}
} catch {
print("Failed to retrieve file attributes: \(error.localizedDescription)")
print("Failed to get file size: \(error.localizedDescription)")
}

return (name, size)
return "Unknown"
}


func getCreationDate(at url: URL) -> String {
do {
let attributes = try fileManager.attributesOfItem(atPath: url.path)
Expand All @@ -132,52 +136,6 @@ class FileManagerViewModel: ObservableObject {
return "Unknown"
}


//func getFileType(at url: URL) -> String {
//return (try? url.resourceValues(forKeys: [.contentTypeKey]).contentType?.localizedDescription) ?? //"Unknown"
//}

//func getMIMEType(at url: URL) -> String {
//guard let pathExtension = url.pathExtension as CFString?,
//let uti = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, nil)?.takeRetainedValue(),
//let mimeType = UTTypeCopyPreferredTagWithClass(uti, kUTTagClassMIMEType)?.takeRetainedValue() else {
//return //"application/octet-stream"
//}
//return mimeType as String
//}

//func getFileHash(at url: URL, hashType: HashType) -> String {
//do {
//let data = try Data(contentsOf: url)
//var hash: [UInt8]
////var length: Int32
//switch hashType {
//case .md5:
//hash = [UInt8](repeating: 0, count: Int(CC_MD5_DIGEST_LENGTH))
//length = CC_MD5_DIGEST_LENGTH
//data.withUnsafeBytes {
//_ = CC_MD5($0.baseAddress, CC_LONG(data.count), &hash)
//}
//case .sha1:
//hash = [UInt8](repeating: 0, count: Int(CC_SHA1_DIGEST_LENGTH))
//length = CC_SHA1_DIGEST_LENGTH
//data.withUnsafeBytes {
//_ = CC_SHA1($0.baseAddress, CC_LONG(data.count), &hash)
//}
//case .sha256:
//hash = [UInt8](repeating: 0, count: Int(CC_SHA256_DIGEST_LENGTH))
//length = CC_SHA256_DIGEST_LENGTH
//data.withUnsafeBytes {
//_ = CC_SHA256($0.baseAddress, CC_LONG(data.count), &hash)
//}
//}
//return hash.map { String(format: "%02hhx", $0) }.joined()
//} catch {
//print(//"Failed to get file hash: \(error.localizedDescription)")
//return //"Unknown"
//}
//}

func isPermissionGranted(for url: URL, permission: FilePermission) -> Bool {
do {
let attributes = try FileManager.default.attributesOfItem(atPath: url.path)
Expand Down Expand Up @@ -288,7 +246,7 @@ class FileManagerViewModel: ObservableObject {
isSearching = false
}

func sortItems() {
private func sortItems() {
switch sortOption {
case .name:
items.sort { $0.name.lowercased() < $1.name.lowercased() }
Expand All @@ -303,7 +261,7 @@ class FileManagerViewModel: ObservableObject {
filterItems()
}

func filterItems() {
private func filterItems() {
let sourceItems: [FileSystemItem]
switch searchScope {
case .current:
Expand Down Expand Up @@ -379,7 +337,3 @@ class FileManagerViewModel: ObservableObject {
}
}
}

enum HashType {
case md5, sha1, sha256
}
6 changes: 4 additions & 2 deletions FilePermissionView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ struct FilePermissionView: View {

return List {
Section(header: Text("File Metadata")) {
Text("Name: \(metadata.name)")
Text("Size: \(metadata.size)")
Text("Name: \(metadata["Name"] ?? "Unknown")")
Text("Size: \(metadata["Size"] ?? "Unknown")")
Text("Creation Date: \(metadata["Creation Date"] ?? "Unknown")")
Text("Modification Date: \(metadata["Modification Date"] ?? "Unknown")")
}

Section(header: Text("File Permissions")) {
Expand Down
2 changes: 1 addition & 1 deletion Resources/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<key>CFBundleVersion</key>
<string>1.0</string>
<key>MinimumOSVersion</key>
<string>14.0</string>
<string>15.0</string>
<key>UILaunchScreen</key>
<dict>
<key>UILaunchScreen</key>
Expand Down
26 changes: 26 additions & 0 deletions UIRefreshControl Wrapper.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//
// UIViewControllerRepresentable.swift
//
// Created by Speedyfriend67 on 04.07.24
//

import SwiftUI

struct RefreshableScrollView<Content: View>: View {
let content: () -> Content
let onRefresh: () -> Void

init(@ViewBuilder content: @escaping () -> Content, onRefresh: @escaping () -> Void) {
self.content = content
self.onRefresh = onRefresh
}

var body: some View {
ScrollView {
content()
}
.refreshable {
onRefresh()
}
}
}

0 comments on commit 95cba46

Please sign in to comment.