Skip to content

Commit

Permalink
处理兼容多个api key
Browse files Browse the repository at this point in the history
  • Loading branch information
GL-Wen committed Mar 27, 2018
1 parent ea315b9 commit 60d277e
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
29 changes: 16 additions & 13 deletions source/tinypng4mac/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="12120" systemVersion="16E195" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<document type="com.apple.InterfaceBuilder3.Cocoa.Storyboard.XIB" version="3.0" toolsVersion="13771" targetRuntime="MacOSX.Cocoa" propertyAccessControl="none" useAutolayout="YES" initialViewController="B8D-0N-5wS">
<dependencies>
<deployment identifier="macosx"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="12120"/>
<plugIn identifier="com.apple.InterfaceBuilder.CocoaPlugin" version="13771"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
Expand Down Expand Up @@ -144,6 +144,9 @@
<rect key="screenRect" x="0.0" y="0.0" width="1680" height="1027"/>
<value key="minSize" type="size" width="320" height="320"/>
<value key="maxSize" type="size" width="320" height="400"/>
<connections>
<outlet property="delegate" destination="B8D-0N-5wS" id="03d-9z-oY8"/>
</connections>
</window>
<connections>
<segue destination="f1c-SR-qaC" kind="relationship" relationship="window.shadowedContentViewController" id="b1L-a1-5Ds"/>
Expand All @@ -164,31 +167,31 @@
<visualEffectView appearanceType="vibrantLight" blendingMode="behindWindow" material="appearanceBased" state="active" translatesAutoresizingMaskIntoConstraints="NO" id="6R4-nY-g9G">
<rect key="frame" x="0.0" y="0.0" width="320" height="100"/>
<subviews>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="j7S-Iz-wE1">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" translatesAutoresizingMaskIntoConstraints="NO" id="j7S-Iz-wE1">
<rect key="frame" x="18" y="42" width="104" height="16"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Output Path:" id="wAc-5q-1kG">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="NnK-w1-6b9">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" translatesAutoresizingMaskIntoConstraints="NO" id="NnK-w1-6b9">
<rect key="frame" x="18" y="16" width="104" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="Replace Origion:" id="7il-ST-uVu">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="iOT-w7-ALe">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" horizontalCompressionResistancePriority="752" translatesAutoresizingMaskIntoConstraints="NO" id="iOT-w7-ALe">
<rect key="frame" x="18" y="67" width="104" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" alignment="right" title="API Key:" id="tJg-5O-cpK">
<font key="font" metaFont="system"/>
<color key="textColor" name="labelColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fT1-QL-4Nq">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="fT1-QL-4Nq">
<rect key="frame" x="132" y="39" width="144" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="99a-9k-u4D">
<font key="font" metaFont="system"/>
Expand All @@ -210,7 +213,7 @@
<action selector="clickSelectPath:" target="f1c-SR-qaC" id="6zI-cZ-R32"/>
</connections>
</button>
<textField verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="vPz-Tz-wUm">
<textField verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="vPz-Tz-wUm">
<rect key="frame" x="132" y="65" width="172" height="22"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" selectable="YES" editable="YES" sendsActionOnEndEditing="YES" state="on" borderStyle="bezel" drawsBackground="YES" usesSingleLineMode="YES" id="OCF-de-u9Q">
<font key="font" metaFont="system"/>
Expand Down Expand Up @@ -277,7 +280,7 @@
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" image="icon" id="X7r-Gl-0hN"/>
</imageView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" alphaValue="0.40000000000000002" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="IX1-JP-bbZ">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" alphaValue="0.40000000000000002" translatesAutoresizingMaskIntoConstraints="NO" id="IX1-JP-bbZ">
<rect key="frame" x="57" y="197" width="206" height="18"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Drop .JPG/.PNG images here!" id="cXq-XC-8Ss">
<font key="font" metaFont="systemBold" size="14"/>
Expand Down Expand Up @@ -339,7 +342,7 @@
<color key="backgroundColor" white="1" alpha="0.0" colorSpace="deviceWhite"/>
<color key="gridColor" name="gridColor" catalog="System" colorSpace="catalog"/>
<tableColumns>
<tableColumn width="317" minWidth="40" maxWidth="1000" id="5uH-R6-sDa">
<tableColumn identifier="" width="317" minWidth="40" maxWidth="1000" id="5uH-R6-sDa">
<tableHeaderCell key="headerCell" lineBreakMode="truncatingTail" borderStyle="border">
<font key="font" metaFont="smallSystem"/>
<color key="textColor" name="headerTextColor" catalog="System" colorSpace="catalog"/>
Expand All @@ -364,15 +367,15 @@
</constraints>
<imageCell key="cell" refusesFirstResponder="YES" alignment="left" imageScaling="proportionallyDown" id="JHH-DJ-f0H"/>
</imageView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="j8i-Ad-2S2">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="j8i-Ad-2S2">
<rect key="frame" x="70" y="30" width="46" height="18"/>
<textFieldCell key="cell" lineBreakMode="truncatingMiddle" sendsActionOnEndEditing="YES" title="A.png" id="wj2-uY-zLZ">
<font key="font" metaFont="systemBold" size="14"/>
<color key="textColor" name="controlLightHighlightColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="c8G-p9-kpA">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="c8G-p9-kpA">
<rect key="frame" x="70" y="10" width="35" height="16"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="Label" id="LZa-jE-EIr">
<font key="font" metaFont="cellTitle"/>
Expand Down Expand Up @@ -434,15 +437,15 @@
<autoresizingMask key="autoresizingMask"/>
</scroller>
</scrollView>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="fcw-HM-Ub9">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="fcw-HM-Ub9">
<rect key="frame" x="106" y="396" width="107" height="17"/>
<textFieldCell key="cell" scrollable="YES" lineBreakMode="clipping" sendsActionOnEndEditing="YES" title="TinyPNG for Mac" id="8Ac-Uq-uCZ">
<font key="font" metaFont="system"/>
<color key="textColor" name="highlightColor" catalog="System" colorSpace="catalog"/>
<color key="backgroundColor" name="controlColor" catalog="System" colorSpace="catalog"/>
</textFieldCell>
</textField>
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" allowsCharacterPickerTouchBarItem="NO" translatesAutoresizingMaskIntoConstraints="NO" id="D8d-TS-a2j">
<textField horizontalHuggingPriority="251" verticalHuggingPriority="750" translatesAutoresizingMaskIntoConstraints="NO" id="D8d-TS-a2j">
<rect key="frame" x="14" y="113" width="57" height="17"/>
<textFieldCell key="cell" lineBreakMode="truncatingTail" truncatesLastVisibleLine="YES" sendsActionOnEndEditing="YES" title="Reduce: " id="400-FH-fhZ">
<font key="font" metaFont="system"/>
Expand Down
2 changes: 1 addition & 1 deletion source/tinypng4mac/MainViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class MainViewController: NSViewController, NSOpenSavePanelDelegate, NSTableView

if let savedKey = TPConfig.savedkey() {
apiKey.stringValue = savedKey
TPClient.sApiKey = savedKey
TPClient.sApiKey = savedKey
keySaved = true
}
if let savedPath = TPConfig.savedPath() {
Expand Down
21 changes: 19 additions & 2 deletions source/tinypng4mac/tpclient/TPClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@ class TPClient {
let BASE_URL = "https://api.tinify.com/shrink"

static let sharedClient = TPClient()
static var sApiKey = ""
static var sApiKeys = [String]()
static var sApiKey = "" {
didSet {
sApiKeys = sApiKey.components(separatedBy: ",")
}
}
static var sOutputPath = "" {
didSet {
IOHeler.sOutputPath = sOutputPath
Expand All @@ -34,6 +39,8 @@ class TPClient {
let lock: NSLock = NSLock()
var runningTasks = 0
var finishTasksCount = 0
var taskIndex = 0


func add(_ tasks: [TPTaskInfo]) {
TPStore.sharedStore.add(tasks);
Expand Down Expand Up @@ -66,7 +73,17 @@ class TPClient {
let fileHandler = try FileHandle(forReadingFrom:task.originFile as URL)
imageData = fileHandler.readDataToEndOfFile()

let auth = "api:\(TPClient.sApiKey)"
var auth = ""
if TPClient.sApiKeys.count != 0 {
let key = TPClient.sApiKeys[taskIndex]
auth = "api:\(key)"

taskIndex += 1
if taskIndex >= TPClient.sApiKeys.count {
taskIndex = 0
}
}

let authData = auth.data(using: String.Encoding.utf8)?.base64EncodedString(options: NSData.Base64EncodingOptions.lineLength64Characters)
let authorizationHeader = "Basic " + authData!

Expand Down
7 changes: 4 additions & 3 deletions source/tinypng4mac/views/DragContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ class DragContainer: NSView {
let manager = FileManager.default

for path in board {
let url = URL(fileURLWithPath: path as! String)
let url = URL(fileURLWithPath: path as! String)

if self.isFolder(path as! String) {
//查找目录下所有的资源文件
// let directorys = try? manager.contentsOfDirectory(atPath: path as! String)
Expand Down Expand Up @@ -105,7 +105,8 @@ class DragContainer: NSView {
var isFolder:ObjCBool = false;

let manager = FileManager.default
return manager.fileExists(atPath: path, isDirectory:&isFolder);
manager.fileExists(atPath: path, isDirectory:&isFolder);
return isFolder.boolValue
}

func checkExtension(_ draggingInfo: NSDraggingInfo) -> Bool {
Expand Down

0 comments on commit 60d277e

Please sign in to comment.