|
| 1 | +// |
| 2 | +// FileManagerAdditions.swift |
| 3 | +// GIOVANNI |
| 4 | +// |
| 5 | +// Copyright (c) <2017>, Gabriel O'Flaherty-Chan |
| 6 | +// All rights reserved. |
| 7 | +// |
| 8 | +// Redistribution and use in source and binary forms, with or without |
| 9 | +// modification, are permitted provided that the following conditions are met: |
| 10 | +// 1. Redistributions of source code must retain the above copyright |
| 11 | +// notice, this list of conditions and the following disclaimer. |
| 12 | +// 2. Redistributions in binary form must reproduce the above copyright |
| 13 | +// notice, this list of conditions and the following disclaimer in the |
| 14 | +// documentation and/or other materials provided with the distribution. |
| 15 | +// 3. All advertising materials mentioning features or use of this software |
| 16 | +// must display the following acknowledgement: |
| 17 | +// This product includes software developed by skysent. |
| 18 | +// 4. Neither the name of the skysent nor the |
| 19 | +// names of its contributors may be used to endorse or promote products |
| 20 | +// derived from this software without specific prior written permission. |
| 21 | +// |
| 22 | +// THIS SOFTWARE IS PROVIDED BY skysent ''AS IS'' AND ANY |
| 23 | +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 24 | +// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 25 | +// DISCLAIMED. IN NO EVENT SHALL skysent BE LIABLE FOR ANY |
| 26 | +// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 27 | +// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 28 | +// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 29 | +// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 30 | +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 | +// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | +// |
| 33 | + |
| 34 | +import Foundation |
| 35 | + |
| 36 | +extension String { |
| 37 | + var isValidROMExtension: Bool { |
| 38 | + return ["gb", "gbc", "zip"].contains(self) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +extension FileManager { |
| 43 | + |
| 44 | + enum FileError: LocalizedError { |
| 45 | + case invalidExtension |
| 46 | + |
| 47 | + public var errorDescription: String? { |
| 48 | + switch self { |
| 49 | + case .invalidExtension: |
| 50 | + return "Not a valid ROM file" |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + var documentsDirectory: URL? { |
| 56 | + let directory: FileManager.SearchPathDirectory = .documentDirectory |
| 57 | + return FileManager.default.urls(for: directory, in: .userDomainMask).first as URL? |
| 58 | + } |
| 59 | + |
| 60 | + func receiveFile(at fileURL: URL, completion: ((String) -> Bool), failure: ((Error) -> Bool)) -> Bool { |
| 61 | + |
| 62 | + guard fileURL.pathExtension.isValidROMExtension else { |
| 63 | + return failure(FileError.invalidExtension) |
| 64 | + } |
| 65 | + |
| 66 | + do { |
| 67 | + let name = fileURL.lastPathComponent |
| 68 | + let destinationPath = documentsDirectory!.appendingPathComponent(name) |
| 69 | + try FileManager.default.moveItem(at: fileURL, to: destinationPath) |
| 70 | + return completion(name) |
| 71 | + } catch (let error) { |
| 72 | + return failure(error) |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments