Skip to content

Instantly share code, notes, and snippets.

@tsuyukimakoto
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tsuyukimakoto/57f91a1ff30b56e3ce4d to your computer and use it in GitHub Desktop.
Save tsuyukimakoto/57f91a1ff30b56e3ce4d to your computer and use it in GitHub Desktop.
//
// main.swift
// plain_directory
//
// Created by makoto tsuyuki on 2014/08/23.
// Copyright (c) 2014年 makoto tsuyuki. All rights reserved.
//
import Foundation
import Cocoa
let _importBasePath = "/Volumes/PATH/TO/ApertureMasters"
let _exportBasePath = "/Volumes/PATH/TO/MyMedia/"
var formatter = NSDateFormatter()
let jst = NSTimeZone(name: "JST")
let utc = NSTimeZone(name: "UTC")
var output_formatter = NSDateFormatter()
output_formatter.timeZone = utc
output_formatter.dateFormat = "yyyy/MM/dd/"
let nine = NSTimeInterval(60*60*9)
let exifType = ["jpeg", "jpg", "tiff", "dng", "raf", "nef", "tif"]
let all = ["jpeg", "jpg", "png", "3gp", "gif", "tiff", "mov", "mp4", "m4v", "jp2", "dng", "avi", "raf", "nef", "tif"]
var fm = NSFileManager()
var tree_fm = NSFileManager()
var err:NSError?
var err2:NSError?
var copy_cnt = 0
func copy_file(arg:String) {
let _arg = arg.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)
let _ext = arg.pathExtension.lowercaseString
let _filename = arg.lastPathComponent
var _create_date:NSDate!
if (all.filter{$0 == _ext}.count == 0) {
return
}
let d = fm.attributesOfItemAtPath(arg, error: &err2)
var _file_creation_date = d["NSFileCreationDate"] as NSDate
var _file_modification_date = d["NSFileModificationDate"] as NSDate
if (exifType.filter{$0 == _ext}.count > 0) {
let cgDataRef = CGImageSourceCreateWithURL(NSURL.URLWithString("file://\(_arg)"), nil).takeUnretainedValue()
let imageDict:CFDictionaryRef = CGImageSourceCopyPropertiesAtIndex(cgDataRef, 0, nil).takeUnretainedValue()
let _d = imageDict.__conversion()
if let _exif:NSMutableDictionary = _d["{Exif}"] as? NSMutableDictionary {
if let _date_time_original: NSString = _exif["DateTimeOriginal"] as? NSString {
formatter.dateFormat = "yyyy:MM:dd HH:mm:ss"
formatter.timeZone = utc
_create_date = formatter.dateFromString(_date_time_original)
}
}
}
if (_create_date == nil && all.filter{$0 == _ext}.count > 0) {
formatter.dateFormat = "yyyy:MM:dd HH:mm:ss +zzzz"
formatter.timeZone = utc
_create_date = d["NSFileModificationDate"] as NSDate
_create_date = _create_date.dateByAddingTimeInterval(nine)
}
let output_directory_path = "\(_exportBasePath)\(output_formatter.stringFromDate(_create_date))"
let output_file_path = "\(_exportBasePath)\(output_formatter.stringFromDate(_create_date))\(_filename)"
println("\(copy_cnt): \(_create_date) => \(_exportBasePath)\(output_formatter.stringFromDate(_create_date))\(_filename)")
if (!fm.fileExistsAtPath(output_directory_path)) {
fm.createDirectoryAtPath(output_directory_path, withIntermediateDirectories: true, attributes: nil, error: &err)
if (err != nil) {
println(err)
}
}
if (!fm.fileExistsAtPath(output_file_path)) {
fm.copyItemAtPath(arg, toPath:output_file_path, error: &err)
if (err != nil) {
println(copy_cnt)
println(err)
fm = NSFileManager()
fm.copyItemAtPath(arg, toPath:output_file_path, error: &err)
if (err != nil) {
println("oh my god!")
}
} else {
var d2:NSDictionary = fm.attributesOfItemAtPath(output_file_path, error: &err2)
if (err2 == nil) {
var md:NSMutableDictionary = d2.mutableCopy() as NSMutableDictionary
md.setValue(_file_creation_date, forKey: "NSFileCreationDate")
md.setValue(_file_modification_date, forKey: "NSFileModificationDate")
fm.setAttributes(md, ofItemAtPath: output_file_path, error: &err2)
} else {
println(err2)
err2 = nil
}
}
} else {
println("\(output_file_path) is exist.")
}
}
func copy_tree(basePath:String) {
println(basePath)
if (copy_cnt % 100 == 0) {
println("copied: \(copy_cnt)")
}
var lst = tree_fm.contentsOfDirectoryAtPath(basePath, error: &err)
if (err != nil) {
println("count: \(copy_cnt)")
println(err2)
}
for v in lst {
let pth = basePath + "/" + (v as String)
let d = tree_fm.attributesOfItemAtPath(pth, error: &err2)
if (err2 != nil) {
println("count: \(copy_cnt)")
println(err2)
}
if (d["NSFileType"] as NSString == "NSFileTypeDirectory") {
fm = NSFileManager()
copy_tree(pth)
} else {
copy_file(pth)
copy_cnt += 1
}
}
}
var rl:rlimit = rlimit(rlim_cur: 1000000,rlim_max: 1000000)
// worthless?
setrlimit(RLIMIT_NOFILE, &rl)
copy_tree(_importBasePath)
//for arg in Process.arguments[1...Process.arguments.count - 1] {
// copy_file(arg)
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment