@@ -23,7 +23,9 @@ public extension File {
2323 ////////////////////////////////////////////////////////////////////////////////
2424 // MARK: - Duplication
2525
26- @discardableResult public func createDuplicate( withName newName: String , keepExtension: Bool = true ) throws -> File {
26+ @discardableResult public func createDuplicate( withName newName: String , keepExtension: Bool = true , overwrite: Bool = true ) throws -> File ? {
27+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
28+
2729 guard let parent = parent else {
2830 throw OperationError . renameFailed ( self )
2931 }
@@ -85,43 +87,58 @@ public extension File {
8587 ////////////////////////////////////////////////////////////////////////////////
8688 // MARK: - Resize image
8789
88- public func resizeImage( newName: String , size: CGSize ) {
90+ @discardableResult public func resizeImage( newName: String , size: CGSize , overwrite: Bool = true ) throws -> File ? {
91+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
8992 run ( " /usr/local/bin/convert " , path, " -resize " , " \( size. width) x \( size. height) " , newName)
93+ return try File ( path: newName)
9094 }
9195
92- public func resizeAt123x( width: Int , height: Int , outputDir: Folder ) throws {
96+ public func resizeAt123x( width: Int , height: Int , outputDir: Folder , overwrite : Bool = true ) throws {
9397 print ( name)
9498
9599 let res1name = outputDir. path. appendingPathComponent ( path: name)
96- resizeImage ( newName: res1name, size: CGSize ( width: width, height: height) )
100+ try resizeImage ( newName: res1name, size: CGSize ( width: width, height: height) , overwrite : overwrite )
97101
98102 let res2name = outputDir. path. appendingPathComponent ( path: nameExcludingExtension + " @2x. " + ( self . extension ?? " " ) )
99- resizeImage ( newName: res2name, size: CGSize ( width: 2 * width, height: 2 * height) )
103+ try resizeImage ( newName: res2name, size: CGSize ( width: 2 * width, height: 2 * height) , overwrite : overwrite )
100104
101105 let res3name = outputDir. path. appendingPathComponent ( path: nameExcludingExtension + " @3x. " + ( self . extension ?? " " ) )
102- resizeImage ( newName: res3name, size: CGSize ( width: 3 * width, height: 3 * height) )
106+ try resizeImage ( newName: res3name, size: CGSize ( width: 3 * width, height: 3 * height) , overwrite : overwrite )
103107 }
104108
105109 ////////////////////////////////////////////////////////////////////////////////
106110 // MARK: - Audio Processing
107111
108- @discardableResult public func slowDownAudio( newName: String , percent: Float ) throws -> File {
112+ @discardableResult public func slowDownAudio( newName: String , percent: Float , overwrite: Bool = true ) throws -> File ? {
113+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
109114 run ( ScriptToolkit . soxPath, path, newName, " tempo " , " -s " , String ( percent) )
110115 return try File ( path: newName)
111116 }
112117
113- @discardableResult public func convertToWav( newName: String ) throws -> File {
118+ @discardableResult public func convertToWav( newName: String , overwrite: Bool = true ) throws -> File ? {
119+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
114120 run ( ScriptToolkit . ffmpegPath, " -i " , path, " -sample_rate " , " 44100 " , newName. deletingPathExtension + " .wav " )
115121 return try File ( path: newName)
116122 }
117123
118- @discardableResult public func convertToM4A( newName: String ) throws -> File {
124+ @discardableResult public func convertToM4A( newName: String , overwrite: Bool = true ) throws -> File ? {
125+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
119126 run ( ScriptToolkit . ffmpegPath, " -i " , path, " -sample_rate " , " 44100 " , newName. deletingPathExtension + " .m4a " )
120127 return try File ( path: newName)
121128 }
122129
123- @discardableResult public func addSilence( newName: String ) throws -> File {
130+ @discardableResult public func addSilence( newName: String , overwrite: Bool = true ) throws -> File ? {
131+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
124132 run ( ScriptToolkit . soxPath, ScriptToolkit . silenceFilePath, path, newName)
125133 return try File ( path: newName)
126134 }
135+
136+ ////////////////////////////////////////////////////////////////////////////////
137+ // MARK: - PDF
138+
139+ @discardableResult public func cropPDF( newName: String , insets: NSEdgeInsets , overwrite: Bool = true ) throws -> File ? {
140+ if !overwrite && FileManager . default. fileExists ( atPath: newName) { return nil }
141+ run ( ScriptToolkit . pdfCropPath, " --margins " , insets. left, insets. top, insets. right, insets. bottom, path, newName)
142+ return try File ( path: newName)
143+ }
127144}
0 commit comments