Skip to content
Permalink
Browse files

Support importing folders via iTunes and airdrop

  • Loading branch information...
GianniCarlo committed Apr 8, 2019
1 parent 33229cb commit c7494cbb2bd18614ace08dc85b1a5b6eb824b9fb
@@ -254,6 +254,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func setupDocumentListener() {
let documentsUrl = DataManager.getDocumentsFolderURL()
self.watcher = DirectoryWatcher.watch(documentsUrl)
self.watcher?.ignoreDirectories = false
self.watcher?.onNewFiles = { newFiles in
for url in newFiles {
DataManager.processFile(at: url)
@@ -30,6 +30,16 @@
<string>com.pkware.zip-archive</string>
</array>
</dict>
<dict>
<key>CFBundleTypeIconFiles</key>
<array/>
<key>CFBundleTypeName</key>
<string>Folders</string>
<key>LSItemContentTypes</key>
<array>
<string>public.folder</string>
</array>
</dict>
</array>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
@@ -170,7 +180,7 @@
<key>CFBundlePackageType</key>
<string>APPL</string>
<key>CFBundleShortVersionString</key>
<string>3.3.0</string>
<string>3.3.1</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleURLTypes</key>
@@ -187,7 +197,7 @@
</dict>
</array>
<key>CFBundleVersion</key>
<string>4</string>
<string>2</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
@@ -20,6 +20,10 @@ class ImportManager {
func process(_ fileUrl: URL, destinationFolder: URL) {
guard !self.files.contains(where: { $0.originalUrl == fileUrl }) else { return }

// Avoid processing the creation of the Processed and Inbox folder
if fileUrl.lastPathComponent == DataManager.processedFolderName
|| fileUrl.lastPathComponent == "Inbox" { return }

self.setupTimer()

let file = FileItem(originalUrl: fileUrl, processedUrl: nil, destinationFolder: destinationFolder)
@@ -23,39 +23,48 @@ class ImportOperation: Operation {
}

func handleZip(file: FileItem) {
guard file.originalUrl.pathExtension == "zip" else { return }

// Unzip to a temp directory
if file.originalUrl.pathExtension == "zip" {
let tempURL = file.destinationFolder.appendingPathComponent("tmp")
let tempURL = file.destinationFolder.appendingPathComponent("tmp")

do {
try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.unzipItem(at: file.originalUrl, to: tempURL)
do {
try FileManager.default.createDirectory(at: tempURL, withIntermediateDirectories: true, attributes: nil)
try FileManager.default.unzipItem(at: file.originalUrl, to: tempURL)

} catch {
print("Extraction of ZIP archive failed with error:\(error)")
return
}
} catch {
print("Extraction of ZIP archive failed with error:\(error)")
return
}

let fileManager = FileManager.default
let resourceKeys: [URLResourceKey] = [.creationDateKey, .isDirectoryKey]
let enumerator = fileManager.enumerator(at: tempURL,
includingPropertiesForKeys: resourceKeys,
options: [.skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
print("directoryEnumerator error at \(url): ", error)
return true
})!

for case let fileURL as URL in enumerator {
if fileURL.pathExtension == "mp3" || fileURL.pathExtension == "m4a" || fileURL.pathExtension == "m4b" {
let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
let destinationURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent)
try? FileManager.default.moveItem(at: fileURL, to: destinationURL)
}
}
let tempItem = FileItem(originalUrl: tempURL, processedUrl: nil, destinationFolder: file.destinationFolder)

self.handleDirectory(file: tempItem)

// Delete original zip file
try? FileManager.default.removeItem(at: file.originalUrl)
}

func handleDirectory(file: FileItem) {
let resourceKeys: [URLResourceKey] = [.creationDateKey, .isDirectoryKey]
let enumerator = FileManager.default.enumerator(at: file.originalUrl,
includingPropertiesForKeys: resourceKeys,
options: [.skipsHiddenFiles], errorHandler: { (url, error) -> Bool in
print("directoryEnumerator error at \(url): ", error)
return true
})!

// Delete temp directory
try? FileManager.default.removeItem(at: tempURL)
let documentsURL = DataManager.getDocumentsFolderURL()

for case let fileURL as URL in enumerator {
guard fileURL.pathExtension == "mp3" || fileURL.pathExtension == "m4a" || fileURL.pathExtension == "m4b" else { continue }

let destinationURL = documentsURL.appendingPathComponent(fileURL.lastPathComponent)
try? FileManager.default.moveItem(at: fileURL, to: destinationURL)
}

// Delete directory
try? FileManager.default.removeItem(at: file.originalUrl)
}

override func main() {
@@ -67,6 +76,15 @@ class ImportOperation: Operation {
continue
}

guard let attributes = try? FileManager.default.attributesOfItem(atPath: file.originalUrl.path) else {
continue
}

if let type = attributes[.type] as? FileAttributeType, type == .typeDirectory {
handleDirectory(file: file)
continue
}

guard FileManager.default.fileExists(atPath: file.originalUrl.path),
let inputStream = InputStream(url: file.originalUrl) else {
continue

0 comments on commit c7494cb

Please sign in to comment.
You can’t perform that action at this time.