<commit_before>//
//  FilePathSelectorViewController.swift
//  Sex8BlockExtension
//
//  Created by virus1994 on 2017/7/16.
//  Copyright © 2017年 ascp. All rights reserved.
//

import Cocoa

let ImageFilePathKey = "imageFilePath"

class FilePathSelectorViewController: NSViewController {
    @IBOutlet weak var filePath: NSTextField!
    let defaultSet = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        do {
            let url = try FileManager.default.url(for: .userDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
            filePath.stringValue = url.path
            readAllImageAndSave(specifcalURL: url)
        } catch {
            print("url fetch error!: \(error.localizedDescription)")
            let alert = NSAlert()
            alert.addButton(withTitle: "确定")
            alert.messageText = "图片路径读取失败"
            alert.informativeText = " \(error.localizedDescription)"
            alert.alertStyle = .warning
            alert.beginSheetModal(for: view.window!, completionHandler: {
                code in
                switch code {
                case NSApplication.ModalResponse.alertFirstButtonReturn:
                    self.view.window?.close()
                    break
                default:
                    break
                }
            })
        }
        
//        if let dir = defaultSet.url(forKey: ImageFilePathKey) {
//            filePath.stringValue = dir.path
//        }
    }
    
    override func viewDidDisappear() {
        super.viewDidDisappear()
//        NSApp.stopModal()
    }
    
    @IBAction func modifyPath(_ sender: Any) {
//        let savePanel = NSSavePanel()
//        savePanel.message = "选择图片保存的路径"
//        savePanel.nameFieldLabel = "文件夹路径"
//        savePanel.allowedFileTypes = ["jpg", "jpeg", "png"]
//        savePanel.allowsOtherFileTypes = true
//        savePanel.canCreateDirectories = true
//        
//        savePanel.beginSheetModal(for: NSApplication.shared().keyWindow!, completionHandler: {
//            response in
//            if response == NSFileHandlingPanelOKButton, let dir = savePanel.directoryURL {
//                print(dir)
//                self.defaultSet.set(dir, forKey: ImageFilePathKey)
//                self.readAllImageAndSave(specifcalURL: dir)
//            }
//        })
        
//        let openPanel = NSOpenPanel()
//        openPanel.canChooseDirectories = true
//        openPanel.canChooseFiles = false
//        openPanel.allowsMultipleSelection = false
//        openPanel.canCreateDirectories = true
//        openPanel.beginSheetModal(for: NSApplication.shared().keyWindow!, completionHandler: {
//            response in
//            if response == NSFileHandlingPanelOKButton, let dir = openPanel.directoryURL {
//                print(dir)
//                self.defaultSet.set(dir, forKey: ImageFilePathKey)
//                self.readAllImageAndSave()
//            }
//        })
    }
    
    func readAllImageAndSave(specifcalURL: URL?) {
        let app = NSApplication.shared.delegate as! AppDelegate
        let managedObjectContext = app.managedObjectContext
        let employeesFetch = NSFetchRequest<NetDisk>(entityName: "NetDisk")
        let sort = NSSortDescriptor(key: "creattime", ascending: false)
        employeesFetch.sortDescriptors = [sort]
        
        do {
            let datas = try managedObjectContext.fetch(employeesFetch)
            let manager = FileManager.default
            if let pictureDomain = specifcalURL?.path {
                for net in datas {
                    let netName = net.title ?? UUID().uuidString
                    let netPath = pictureDomain + "/" + netName.replacingOccurrences(of: "/", with: "|")
                    if !manager.fileExists(atPath: netPath) {
                        try manager.createDirectory(atPath: netPath, withIntermediateDirectories: false, attributes: nil)
                    }
                    
                    for pic in net.pic?.allObjects as? [Pic] ?? [] {
                        if let img = pic.data, let urlString = pic.pic, let url = URL(string: urlString) {
                            let imgData = img as Data
                            let file = netPath + "/" + url.lastPathComponent
                            if !manager.fileExists(atPath: file), manager.createFile(atPath: file, contents: imgData, attributes: nil) {
                                print("save image:" + file)
                            }   else    {
                                print("save image:" + file + " faild!")
                            }
                        }
                    }
                    
                }
            }
            self.view.window?.close()
        } catch {
            fatalError("Failed to fetch employees: \(error)")
        }
    }
}
<commit_msg>1.change function to show path of images<commit_after>//
//  FilePathSelectorViewController.swift
//  Sex8BlockExtension
//
//  Created by virus1994 on 2017/7/16.
//  Copyright © 2017年 ascp. All rights reserved.
//

import Cocoa

let ImageFilePathKey = "imageFilePath"

class FilePathSelectorViewController: NSViewController {
    @IBOutlet weak var filePath: NSTextField!
    let defaultSet = UserDefaults.standard
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do view setup here.
        do {
            let url = try FileManager.default.url(for: .picturesDirectory, in: .userDomainMask, appropriateFor: nil, create: false)
            filePath.stringValue = url.appendingPathExtension("sex8").path
//            readAllImageAndSave(specifcalURL: url)
        } catch {
            print("url fetch error!: \(error.localizedDescription)")
            DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 2, execute: {
                let alert = NSAlert()
                alert.addButton(withTitle: "确定")
                alert.messageText = "图片路径读取失败"
                alert.informativeText = " \(error.localizedDescription)"
                alert.alertStyle = .warning
                alert.beginSheetModal(for: NSApplication.shared.keyWindow!, completionHandler: {
                    code in
                    switch code {
                    case NSApplication.ModalResponse.alertFirstButtonReturn:
                        self.view.window?.close()
                        break
                    default:
                        break
                    }
                })
            })
        }
        
//        if let dir = defaultSet.url(forKey: ImageFilePathKey) {
//            filePath.stringValue = dir.path
//        }
    }
    
    override func viewDidDisappear() {
        super.viewDidDisappear()
//        NSApp.stopModal()
    }
    
    @IBAction func modifyPath(_ sender: Any) {
//        let savePanel = NSSavePanel()
//        savePanel.message = "选择图片保存的路径"
//        savePanel.nameFieldLabel = "文件夹路径"
//        savePanel.allowedFileTypes = ["jpg", "jpeg", "png"]
//        savePanel.allowsOtherFileTypes = true
//        savePanel.canCreateDirectories = true
//        
//        savePanel.beginSheetModal(for: NSApplication.shared().keyWindow!, completionHandler: {
//            response in
//            if response == NSFileHandlingPanelOKButton, let dir = savePanel.directoryURL {
//                print(dir)
//                self.defaultSet.set(dir, forKey: ImageFilePathKey)
//                self.readAllImageAndSave(specifcalURL: dir)
//            }
//        })
        
//        let openPanel = NSOpenPanel()
//        openPanel.canChooseDirectories = true
//        openPanel.canChooseFiles = false
//        openPanel.allowsMultipleSelection = false
//        openPanel.canCreateDirectories = true
//        openPanel.beginSheetModal(for: NSApplication.shared().keyWindow!, completionHandler: {
//            response in
//            if response == NSFileHandlingPanelOKButton, let dir = openPanel.directoryURL {
//                print(dir)
//                self.defaultSet.set(dir, forKey: ImageFilePathKey)
//                self.readAllImageAndSave()
//            }
//        })
    }
    
    func readAllImageAndSave(specifcalURL: URL?) {
        let app = NSApplication.shared.delegate as! AppDelegate
        let managedObjectContext = app.managedObjectContext
        let employeesFetch = NSFetchRequest<NetDisk>(entityName: "NetDisk")
        let sort = NSSortDescriptor(key: "creattime", ascending: false)
        employeesFetch.sortDescriptors = [sort]
        
        do {
            let datas = try managedObjectContext.fetch(employeesFetch)
            let manager = FileManager.default
            if let pictureDomain = specifcalURL?.path {
                for net in datas {
                    let netName = net.title ?? UUID().uuidString
                    let netPath = pictureDomain + "/" + netName.replacingOccurrences(of: "/", with: "|")
                    if !manager.fileExists(atPath: netPath) {
                        try manager.createDirectory(atPath: netPath, withIntermediateDirectories: false, attributes: nil)
                    }
                    
                    for pic in net.pic?.allObjects as? [Pic] ?? [] {
                        if let img = pic.data, let urlString = pic.pic, let url = URL(string: urlString) {
                            let imgData = img as Data
                            let file = netPath + "/" + url.lastPathComponent
                            if !manager.fileExists(atPath: file), manager.createFile(atPath: file, contents: imgData, attributes: nil) {
                                print("save image:" + file)
                            }   else    {
                                print("save image:" + file + " faild!")
                            }
                        }
                    }
                    
                }
            }
            self.view.window?.close()
        } catch {
            fatalError("Failed to fetch employees: \(error)")
        }
    }
}

{'fraction_non_alphanumeric': 0.09812067260138477, 'fraction_numerical': 0.0034619188921859545, 'mean_word_length': 1.5885816692268304, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4444661', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2572, 'n_words': 546}