Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
gee1k committed Aug 8, 2019
2 parents 8b19f05 + 374094d commit c00323b
Show file tree
Hide file tree
Showing 28 changed files with 416 additions and 123 deletions.
4 changes: 4 additions & 0 deletions uPic.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
1675516422AC102700D3EB6F /* NotificationExt.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675516322AC102700D3EB6F /* NotificationExt.swift */; };
1675516722ACAA0600D3EB6F /* UPicUpdater.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675516622ACAA0600D3EB6F /* UPicUpdater.swift */; };
1675516A22ACAA5900D3EB6F /* AppPublic.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675516922ACAA5900D3EB6F /* AppPublic.swift */; };
1675EC5322FB38240038DA33 /* BaseUploaderUtil.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1675EC5222FB38240038DA33 /* BaseUploaderUtil.swift */; };
167D08A822ED88E7000F3BC0 /* AmazonS3ConfigView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 167D08A622ED88C2000F3BC0 /* AmazonS3ConfigView.swift */; };
167D08AA22ED8A27000F3BC0 /* AliyunHostConfig.swift in Sources */ = {isa = PBXBuildFile; fileRef = 167D08A922ED8A27000F3BC0 /* AliyunHostConfig.swift */; };
167D08AC22ED8A4B000F3BC0 /* AliyunRegion.swift in Sources */ = {isa = PBXBuildFile; fileRef = 167D08AB22ED8A4B000F3BC0 /* AliyunRegion.swift */; };
Expand Down Expand Up @@ -195,6 +196,7 @@
1675516322AC102700D3EB6F /* NotificationExt.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NotificationExt.swift; sourceTree = "<group>"; };
1675516622ACAA0600D3EB6F /* UPicUpdater.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UPicUpdater.swift; sourceTree = "<group>"; };
1675516922ACAA5900D3EB6F /* AppPublic.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppPublic.swift; sourceTree = "<group>"; };
1675EC5222FB38240038DA33 /* BaseUploaderUtil.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BaseUploaderUtil.swift; sourceTree = "<group>"; };
167D08A622ED88C2000F3BC0 /* AmazonS3ConfigView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AmazonS3ConfigView.swift; sourceTree = "<group>"; };
167D08A922ED8A27000F3BC0 /* AliyunHostConfig.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunHostConfig.swift; sourceTree = "<group>"; };
167D08AB22ED8A4B000F3BC0 /* AliyunRegion.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AliyunRegion.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -430,6 +432,7 @@
isa = PBXGroup;
children = (
1602ED9622ADEFB200AA8638 /* BaseUploader.swift */,
1675EC5222FB38240038DA33 /* BaseUploaderUtil.swift */,
1672762222AFF655007299C3 /* Host.swift */,
167D7F4822B4D2F500DD0A7A /* HostConfig.swift */,
167D7F4C22B4D43B00DD0A7A /* HostSaveKey.swift */,
Expand Down Expand Up @@ -817,6 +820,7 @@
1675516722ACAA0600D3EB6F /* UPicUpdater.swift in Sources */,
1685AA3522DEC943008FBF1D /* FlippedView.swift in Sources */,
16068C7822AECB34004D39B7 /* PreferencesWindowController.swift in Sources */,
1675EC5322FB38240038DA33 /* BaseUploaderUtil.swift in Sources */,
16995A3422B6008000B1F923 /* UpYunHostConfig.swift in Sources */,
1672762322AFF655007299C3 /* Host.swift in Sources */,
167DDBF122C7784900B03357 /* GithubConfigView.swift in Sources */,
Expand Down
5 changes: 3 additions & 2 deletions uPic/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ class AppDelegate: NSObject, NSApplicationDelegate {

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Insert code here to initialize your application

ConfigManager.shared.firstSetup()

self.resetNewVersionLaunchAtLogin()

indicator.minValue = 0.0
Expand Down Expand Up @@ -212,7 +213,7 @@ extension AppDelegate {
self.uploadFiles([imgData!])
} else if (NSPasteboard.general.types?.first == NSPasteboard.PasteboardType.tiff) {
let imgData = NSPasteboard.general.data(forType: NSPasteboard.PasteboardType.tiff)
if let jpg = imgData?.convertImageDataToJpg() {
if let jpg = imgData?.convertImageData(.jpeg) {
self.uploadFiles([jpg])
}
}
Expand Down
6 changes: 6 additions & 0 deletions uPic/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<customObject id="veO-oa-UPB" customClass="StatusMenuController" customModule="uPic" customModuleProvider="target">
<connections>
<outlet property="checkUpdateMenuItem" destination="yai-4F-Btp" id="klG-hy-GGO"/>
<outlet property="compressFactorMenuItem" destination="eIU-EA-uQZ" id="uMq-Nm-vXy"/>
<outlet property="helpMenuItem" destination="2Jw-ET-3eH" id="Ubq-wz-L3N"/>
<outlet property="historyMenuItem" destination="EhO-Zw-XXj" id="okQ-0q-kK5"/>
<outlet property="hostMenuItem" destination="d02-bT-1Lv" id="Ah0-J0-Z4M"/>
Expand Down Expand Up @@ -165,6 +166,11 @@
</items>
</menu>
</menuItem>
<menuItem isSeparatorItem="YES" id="ijd-GZ-VTQ"/>
<menuItem title="上传前压缩图片" id="eIU-EA-uQZ">
<modifierMask key="keyEquivalentModifierMask"/>
<menu key="submenu" title="上传前压缩图片" id="4yH-6k-LD0"/>
</menuItem>
<menuItem isSeparatorItem="YES" id="bfM-jE-V7Q"/>
<menuItem title="上传历史" id="EhO-Zw-XXj">
<modifierMask key="keyEquivalentModifierMask"/>
Expand Down
42 changes: 26 additions & 16 deletions uPic/Basic/Data+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,35 +10,35 @@ import Foundation
import CryptoSwift

extension Data {

func toBytes() -> Array<UInt8> {
return self.bytes
}

func toMd5() -> String {
return self.md5().toHexString()
}

func toBase64() -> String {
return self.bytes.toBase64()!
}

func toSha1() -> String {
return self.sha1().toHexString()
}

func toSha224() -> String {
return self.sha224().toHexString()
}

func toSha256() -> String {
return self.sha256().toHexString()
}

func toSha384() -> String {
return self.sha384().toHexString()
}

func toSha512() -> String {
return self.sha512().toHexString()
}
Expand All @@ -47,16 +47,27 @@ extension Data {
return String(data: self, encoding: .utf8)!
}

func convertImageDataToJpg() -> Data? {
// 转换图片格式
func convertImageData(_ fileType: NSBitmapImageRep.FileType = .png) -> Data? {
let bitmap = NSBitmapImageRep(data: self)
let jpg = bitmap?.representation(using: .jpeg, properties: [:])
return jpg
let data = bitmap?.representation(using: fileType, properties: [:])
return data
}

func convertImageDataToPng() -> Data? {
let bitmap = NSBitmapImageRep(data: self)
let png = bitmap?.representation(using: .png, properties: [:])
return png
/**
压缩图片,只支持压缩图片,压缩之后图片格式是 jpg。
gif,以及其他非图片均不能压缩
factor: 压缩率 0~1
*/
func compressImage(_ factor: Float = 0.7) -> Data {
guard let bitmap = NSBitmapImageRep(data: self) else {
return self
}
if (factor > 0.0 && factor < 1.0 && self.contentType() != "gif" && bitmap.canBeCompressed(using: .jpeg)) {
let repData = bitmap.representation(using: .jpeg, properties: [.compressionFactor: factor])
return repData ?? self
}
return self
}

func contentType() -> String? {
Expand All @@ -78,5 +89,4 @@ extension Data {
}

}

}
4 changes: 2 additions & 2 deletions uPic/Extensions/NSDragingInfoExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ extension NSDraggingInfo {
var draggedFromBrowserData: Data? {

if let tiff = draggingPasteboard.data(forType: NSPasteboard.PasteboardType.tiff) {
let jpg = tiff.convertImageDataToJpg()
let jpg = tiff.convertImageData(.jpeg)
return jpg
} else if let pdf = draggingPasteboard.data(forType: NSPasteboard.PasteboardType.pdf) {
return pdf
Expand All @@ -56,7 +56,7 @@ extension NSDraggingInfo {
} else if let urlStr = draggingPasteboard.string(forType: NSPasteboard.PasteboardType.string) {
if let url = URL(string: urlStr.urlEncoded()), let image = NSImage(contentsOf: url) {
if image.isValid {
let jpg = image.tiffRepresentation?.convertImageDataToJpg()
let jpg = image.tiffRepresentation?.convertImageData(.jpeg)
return jpg
}

Expand Down
16 changes: 16 additions & 0 deletions uPic/General/Managers/ConfigManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class ConfigManager {
return
}
Defaults[.launchAtLogin] = BoolType._false.rawValue
Defaults[.compressFactor] = 100
Defaults.synchronize()
}

Expand Down Expand Up @@ -130,3 +131,18 @@ extension ConfigManager {
self.setHistoryList(items: [])
}
}

extension ConfigManager {
// MARK: 上传前压缩图片,压缩率
var compressFactor: Int {
get {
return Defaults[.compressFactor] ?? 100
}

set {
Defaults[.compressFactor] = newValue
Defaults.synchronize()
}
}
}

11 changes: 11 additions & 0 deletions uPic/General/Utils/PreferenceKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Keys {
static let ouputFormat = "uPic_OutputFormat"
static let historyList = "uPic_HistoryList"
static let historyLimit = "uPic_HistoryLimit"
static let compressFactor = "uPic_CompressFactor"
}

class DefaultsKeys {
Expand Down Expand Up @@ -45,6 +46,7 @@ extension DefaultsKeys {
static let ouputFormat = DefaultsKey<Int>(Keys.ouputFormat)
static let historyList = DefaultsKey<[String]>(Keys.historyList)
static let historyLimit = DefaultsKey<Int>(Keys.historyLimit)
static let compressFactor = DefaultsKey<Int>(Keys.compressFactor)

}

Expand All @@ -68,6 +70,15 @@ extension UserDefaults {
set(newValue, forKey: key._key)
}
}

subscript(key: DefaultsKey<Float>) -> Float? {
get {
return float(forKey: key._key)
}
set {
set(newValue, forKey: key._key)
}
}

subscript(key: DefaultsKey<[Host]>) -> [Host]? {
get {
Expand Down
10 changes: 7 additions & 3 deletions uPic/Models/Aliyun/AliyunUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,15 @@ class AliyunUploader: BaseUploader {
return
}

var retData = fileData
var fileName = ""
var mimeType = ""
if let fileUrl = fileUrl {
fileName = "\(hostSaveKey.getFileName(filename: fileUrl.lastPathComponent.deletingPathExtension)).\(fileUrl.pathExtension)"
mimeType = Util.getMimeType(pathExtension: fileUrl.pathExtension)
retData = BaseUploaderUtil.compressImage(fileUrl)
} else if let fileData = fileData {
retData = BaseUploaderUtil.compressImage(fileData)
// MARK: 处理截图之类的图片,生成一个文件名
let fileType = fileData.contentType() ?? "png"
fileName = "\(hostSaveKey.getFileName()).\(fileType)"
Expand All @@ -55,6 +58,7 @@ class AliyunUploader: BaseUploader {
return
}


var key = fileName
if (config.folder != nil && config.folder!.count > 0) {
key = "\(config.folder!)/\(key)"
Expand All @@ -79,10 +83,10 @@ class AliyunUploader: BaseUploader {
multipartFormData.append(policy.data(using: .utf8)!, withName: "policy")
multipartFormData.append(signature.data(using: .utf8)!, withName: "Signature")

if fileUrl != nil {
if retData != nil {
multipartFormData.append(retData!, withName: "file", fileName: fileName, mimeType: mimeType)
} else if fileUrl != nil {
multipartFormData.append(fileUrl!, withName: "file", fileName: fileName, mimeType: mimeType)
} else {
multipartFormData.append(fileData!, withName: "file", fileName: fileName, mimeType: mimeType)
}
}

Expand Down
9 changes: 6 additions & 3 deletions uPic/Models/Amazon_S3/AmazonS3Uploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,19 @@ class AmazonS3Uploader: BaseUploader {
return
}

var retData = fileData
var fileName = ""
var mimeType = ""
if let fileUrl = fileUrl {
fileName = "\(hostSaveKey.getFileName(filename: fileUrl.lastPathComponent.deletingPathExtension)).\(fileUrl.pathExtension)"
mimeType = Util.getMimeType(pathExtension: fileUrl.pathExtension)
retData = BaseUploaderUtil.compressImage(fileUrl)
} else if let fileData = fileData {
// MARK: 处理截图之类的图片,生成一个文件名
let fileType = fileData.contentType() ?? "png"
fileName = "\(hostSaveKey.getFileName()).\(fileType)"
mimeType = Util.getMimeType(pathExtension: fileType)
retData = BaseUploaderUtil.compressImage(fileData)
} else {
super.faild(errorMsg: "Invalid file")
return
Expand Down Expand Up @@ -94,10 +97,10 @@ class AmazonS3Uploader: BaseUploader {
// 如不手动设置 content-type , aws 默认会将文件的 content-type 设置为 binary/octet-stream 。访问时将会直接下载,而不是预览
multipartFormData.append(mimeType.data(using: .utf8)!, withName: "content-type")

if fileUrl != nil {
if retData != nil {
multipartFormData.append(retData!, withName: "file", fileName: fileName, mimeType: mimeType)
} else if fileUrl != nil {
multipartFormData.append(fileUrl!, withName: "file", fileName: fileName, mimeType: mimeType)
} else {
multipartFormData.append(fileData!, withName: "file", fileName: fileName, mimeType: mimeType)
}
}

Expand Down
40 changes: 40 additions & 0 deletions uPic/Models/BaseUploaderUtil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
//
// BaseUploaderUtil.swift
// uPic
//
// Created by Svend Jin on 2019/8/8.
// Copyright © 2019 Svend Jin. All rights reserved.
//

import Foundation

class BaseUploaderUtil {
static func compressImage(_ data: Data) -> Data {
var factor:Int = ConfigManager.shared.compressFactor

if factor >= 100 {
return data
}

// 压缩图片
debugPrint("origin data -> \(data.bytes.count)")
let retData = data.compressImage(Float(factor) / 100)
debugPrint("compress data -> \(retData.bytes.count)")
return retData
}

static func compressImage(_ url: URL) -> Data? {
var factor:Int = ConfigManager.shared.compressFactor

if factor >= 100 {
return nil
}

let data = try? Data(contentsOf: url)
// 压缩图片
debugPrint("origin data -> \(data?.bytes.count)")
let retData = data?.compressImage(Float(factor) / 100)
debugPrint("compress data -> \(retData?.bytes.count)")
return retData
}
}
9 changes: 6 additions & 3 deletions uPic/Models/Custom/CustomUploader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,19 @@ class CustomUploader: BaseUploader {
return
}

var retData = fileData
var fileName = ""
var mimeType = ""
if let fileUrl = fileUrl {
fileName = "\(hostSaveKey.getFileName(filename: fileUrl.lastPathComponent.deletingPathExtension)).\(fileUrl.pathExtension)"
mimeType = Util.getMimeType(pathExtension: fileUrl.pathExtension)
retData = BaseUploaderUtil.compressImage(fileUrl)
} else if let fileData = fileData {
// MARK: 处理截图之类的图片,生成一个文件名
let fileType = fileData.contentType() ?? "png"
fileName = "\(hostSaveKey.getFileName()).\(fileType)"
mimeType = Util.getMimeType(pathExtension: fileType)
retData = BaseUploaderUtil.compressImage(fileData)
} else {
super.faild(errorMsg: "Invalid file")
return
Expand Down Expand Up @@ -87,10 +90,10 @@ class CustomUploader: BaseUploader {
}
}

if fileUrl != nil {
if retData != nil {
multipartFormData.append(retData!, withName: field, fileName: fileName, mimeType: mimeType)
} else if fileUrl != nil {
multipartFormData.append(fileUrl!, withName: field, fileName: fileName, mimeType: mimeType)
} else {
multipartFormData.append(fileData!, withName: field, fileName: fileName, mimeType: mimeType)
}
}

Expand Down
Loading

0 comments on commit c00323b

Please sign in to comment.