Skip to content

Commit

Permalink
FileInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleduo committed Sep 25, 2019
1 parent 58ec2ce commit 95eac83
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 24 deletions.
4 changes: 4 additions & 0 deletions source/tinypng4mac.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
27C12A171D35411800F0FD81 /* Localizable.strings in Resources */ = {isa = PBXBuildFile; fileRef = 27C12A151D35411800F0FD81 /* Localizable.strings */; };
27F5BA4B206B7021008529C9 /* TPWindow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F5BA4A206B7021008529C9 /* TPWindow.swift */; };
27F5BA4E206B7373008529C9 /* PasteboardTypeExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 27F5BA4D206B7373008529C9 /* PasteboardTypeExtension.swift */; };
BCFA8CDD233BC9BC0082F4C4 /* FileInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = BCFA8CDC233BC9BC0082F4C4 /* FileInfo.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -60,6 +61,7 @@
3EC4E906FA6863DD0D5839DA /* Pods-TinyPNG4Mac.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TinyPNG4Mac.release.xcconfig"; path = "Pods/Target Support Files/Pods-TinyPNG4Mac/Pods-TinyPNG4Mac.release.xcconfig"; sourceTree = "<group>"; };
811CF203124A83759CC63BFF /* Pods_TinyPNG4Mac.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_TinyPNG4Mac.framework; sourceTree = BUILT_PRODUCTS_DIR; };
B8A32DFEB3E5214A1AADE38D /* Pods-TinyPNG4Mac.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-TinyPNG4Mac.debug.xcconfig"; path = "Pods/Target Support Files/Pods-TinyPNG4Mac/Pods-TinyPNG4Mac.debug.xcconfig"; sourceTree = "<group>"; };
BCFA8CDC233BC9BC0082F4C4 /* FileInfo.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FileInfo.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -126,6 +128,7 @@
isa = PBXGroup;
children = (
277AAF691D254D4F00788F37 /* TPTaskInfo.swift */,
BCFA8CDC233BC9BC0082F4C4 /* FileInfo.swift */,
);
path = model;
sourceTree = "<group>";
Expand Down Expand Up @@ -310,6 +313,7 @@
277AAF6A1D254D4F00788F37 /* TPTaskInfo.swift in Sources */,
277AAF6C1D25517E00788F37 /* TPStore.swift in Sources */,
277AAF621D250DD100788F37 /* DragContainer.swift in Sources */,
BCFA8CDD233BC9BC0082F4C4 /* FileInfo.swift in Sources */,
277AAF781D269D7C00788F37 /* IOHelper.swift in Sources */,
277AAF761D2698B600788F37 /* TPConfig.swift in Sources */,
);
Expand Down
8 changes: 5 additions & 3 deletions source/tinypng4mac/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -212,17 +212,19 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView
func draggingExit() {
}

func draggingFileAccept(_ files:Array<URL>) {
func draggingFileAccept(_ files:Array<FileInfo>) {
if TPClient.sApiKey == "" {
showInputPanel()
return;
}
var tasks = [TPTaskInfo]()
let manager = FileManager.default
for file in files {
let attributes = try? manager.attributesOfItem(atPath: file.path)
print(file.filePath.path)
print(file.filePath.relativePath)
let attributes = try? manager.attributesOfItem(atPath: file.filePath.path)
let size = attributes![FileAttributeKey.size]!
let task = TPTaskInfo(originFile: file, fileName:file.lastPathComponent, originSize: (size as AnyObject).doubleValue!)
let task = TPTaskInfo(file, originSize: (size as AnyObject).doubleValue!)
tasks.append(task)
}
TPClient.sharedClient.add(tasks)
Expand Down
19 changes: 19 additions & 0 deletions source/tinypng4mac/model/FileInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// FileInfo.swift
// TinyPNG4Mac
//
// Created by kyleduo on 2019/9/26.
// Copyright © 2019 kyleduo. All rights reserved.
//

import Foundation

class FileInfo {
var filePath: URL
var relativePath: String

init(_ filePath: URL, relativePath: String) {
self.filePath = filePath
self.relativePath = relativePath
}
}
10 changes: 5 additions & 5 deletions source/tinypng4mac/model/TPTaskInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class TPTaskInfo: NSObject {
var originFile: URL
var outputFile: URL?
var resultUrl: String
var fileName: String
var fileInfo: FileInfo
var originSize: Double
var status: TPTaskStatus
var progress: Progress // progress for uploading and downloading
Expand All @@ -22,9 +22,9 @@ class TPTaskInfo: NSObject {
var errorMessage: String?
var index: Int

init(originFile: URL, fileName: String, originSize: Double) {
self.originFile = originFile
self.fileName = fileName
init(_ fileInfo: FileInfo, originSize: Double) {
self.originFile = fileInfo.filePath
self.fileInfo = fileInfo
self.originSize = originSize

self.status = .initial
Expand All @@ -40,7 +40,7 @@ class TPTaskInfo: NSObject {
}

override var description: String {
return String(format: "Task {}", self.fileName)
return String(format: "Task {}", self.fileInfo.relativePath)
}
}

Expand Down
14 changes: 7 additions & 7 deletions source/tinypng4mac/tpclient/TPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TPClient {
if let t = task {
self.updateStatus(t, newStatus: .prepare)
runningTasks += 1
debugPrint("prepare to upload: " + t.fileName + " tasks: " + String(self.runningTasks))
debugPrint("prepare to upload: " + t.fileInfo.relativePath + " tasks: " + String(self.runningTasks))
executeTask(t)
} else {
break;
Expand All @@ -69,7 +69,7 @@ class TPClient {
let authorizationHeader = "Basic " + authData!

self.updateStatus(task, newStatus: .uploading)
debugPrint("uploading: " + task.fileName)
debugPrint("uploading: " + task.fileInfo.relativePath)

let headers: HTTPHeaders = [
"Authorization": authorizationHeader,
Expand All @@ -79,7 +79,7 @@ class TPClient {
.uploadProgress(closure: { (progress) in
if progress.fractionCompleted == 1 {
self.updateStatus(task, newStatus: .processing)
debugPrint("processing: " + task.fileName)
debugPrint("processing: " + task.fileInfo.relativePath)
} else {
self.updateStatus(task, newStatus: .uploading, progress: progress)
}
Expand All @@ -89,7 +89,7 @@ class TPClient {
let json = JSON(jsonstr)
if json != JSON.null {
if let error = json["error"].string {
debugPrint("error: " + task.fileName + error)
debugPrint("error: " + task.fileInfo.relativePath + error)
self.markError(task, errorMessage: json["message"].string)
return
}
Expand All @@ -116,13 +116,13 @@ class TPClient {
}

fileprivate func onUploadFinish(_ task: TPTaskInfo) {
debugPrint("downloading: " + task.fileName)
debugPrint("downloading: " + task.fileInfo.relativePath)
self.updateStatus(task, newStatus: .downloading)
if TPConfig.shouldReplace() {
task.outputFile = task.originFile;
} else {
let folder = IOHeler.getOutputPath()
task.outputFile = folder.appendingPathComponent(task.fileName)
task.outputFile = folder.appendingPathComponent(task.fileInfo.relativePath)
}
downloadCompressImage(task)
}
Expand All @@ -142,7 +142,7 @@ class TPClient {
self.markError(task, errorMessage: "download error")
} else {
self.updateStatus(task, newStatus: .finish)
debugPrint("finish: " + task.fileName + " tasks: " + String(self.runningTasks))
debugPrint("finish: " + task.fileInfo.relativePath + " tasks: " + String(self.runningTasks))
}

self.checkExecution()
Expand Down
18 changes: 10 additions & 8 deletions source/tinypng4mac/views/DragContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Cocoa
protocol DragContainerDelegate {
func draggingEntered();
func draggingExit();
func draggingFileAccept(_ files:Array<URL>);
func draggingFileAccept(_ files:Array<FileInfo>);
}

class DragContainer: NSView {
Expand Down Expand Up @@ -57,7 +57,7 @@ class DragContainer: NSView {
}

override func performDragOperation(_ sender: NSDraggingInfo) -> Bool {
var files = Array<URL>()
var files = Array<FileInfo>()
if let board = sender.draggingPasteboard.propertyList(forType: NSFilenamesPboardType) as? NSArray {
for path in board {
files.append(contentsOf: collectFiles(path as! String))
Expand All @@ -71,20 +71,22 @@ class DragContainer: NSView {
return true
}

func collectFiles(_ filePath: String) -> Array<URL> {
var files = Array<URL>()
func collectFiles(_ filePath: String) -> Array<FileInfo> {
var files = Array<FileInfo>()
let isDirectory = IOHeler.isDirectory(filePath)
if isDirectory {
let fileManager = FileManager.default
let enumerator = fileManager.enumerator(atPath: filePath)
while let fileName = enumerator?.nextObject() as? String {
let fullFilePath = filePath.appending("/\(fileName)")
while let relativePath = enumerator?.nextObject() as? String {
let fullFilePath = filePath.appending("/\(relativePath)")
if (fileIsAcceptable(fullFilePath)) {
files.append(URL(fileURLWithPath: fullFilePath))
let parent = URL(fileURLWithPath: filePath).lastPathComponent
files.append(FileInfo(URL(fileURLWithPath: fullFilePath), relativePath:"\(parent)/\(relativePath)"))
}
}
} else if (fileIsAcceptable(filePath)) {
files.append(URL(fileURLWithPath: filePath))
let url = URL(fileURLWithPath: filePath)
files.append(FileInfo(url, relativePath:url.lastPathComponent))
}
return files
}
Expand Down
2 changes: 1 addition & 1 deletion source/tinypng4mac/views/TaskTableCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class TaskTableCell: NSTableCellView {

var task: TPTaskInfo? {
didSet {
self.name.stringValue = (task?.fileName)!
self.name.stringValue = (task?.fileInfo.relativePath)!
self.preview.image = NSImage.init(contentsOf: (task?.originFile)! as URL)
let taskStatus = (task?.status)!
var statusText = ""
Expand Down

0 comments on commit 95eac83

Please sign in to comment.