Skip to content

Instantly share code, notes, and snippets.

@pallavtrivedi03
Created March 12, 2018 20:17
Show Gist options
  • Save pallavtrivedi03/a75e69878eb9b2a081bd62dfa8deb12c to your computer and use it in GitHub Desktop.
Save pallavtrivedi03/a75e69878eb9b2a081bd62dfa8deb12c to your computer and use it in GitHub Desktop.
func createDirectory(sourcePath: String, type: MediaType) {
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
// create the custom folder path
var subDirectoryPath = ""
switch type
{
case .audio:
subDirectoryPath = documentDirectoryPath.appending("/audios")
case .video:
subDirectoryPath = documentDirectoryPath.appending("/videos")
case .image:
subDirectoryPath = documentDirectoryPath.appending("/images")
case .document:
subDirectoryPath = documentDirectoryPath.appending("/documents")
}
let fileManager = FileManager.default
if !fileManager.fileExists(atPath: subDirectoryPath) {
do {
try fileManager.createDirectory(atPath: subDirectoryPath,
withIntermediateDirectories: false,
attributes: nil)
let fileName = sourcePath.components(separatedBy: "/").last
let destinationPath = subDirectoryPath.appending("/").appending(fileName!)
moveFileToDocumentsDir(sourcePath: sourcePath, desitinationPath: destinationPath)
} catch {
print("Error creating images folder in documents dir: \(error)")
}
}
else
{
print("Directory exists")
let fileName = sourcePath.components(separatedBy: "/").last
let destinationPath = subDirectoryPath.appending("/").appending(fileName!)
moveFileToDocumentsDir(sourcePath: sourcePath, desitinationPath: destinationPath)
}
}
}
func moveFileToDocumentsDir(sourcePath: String, desitinationPath: String) {
let filemgr = FileManager.default
if !filemgr.fileExists(atPath: desitinationPath)
{
do {
let startIndex = sourcePath.index(sourcePath.startIndex, offsetBy: 7)
let trimmedSourcePath = sourcePath[startIndex...]
try filemgr.moveItem(atPath: String(trimmedSourcePath), toPath: desitinationPath)
}catch let error {
print("Error: \(error.localizedDescription)")
}
}
else
{
print("file exists")
}
}
func getFilesFromDoucmentDir(type: MediaType)
{
// path to documents directory
let documentDirectoryPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first
if let documentDirectoryPath = documentDirectoryPath {
// create the custom folder path
var subDirectoryPath = ""
switch type
{
case .audio:
subDirectoryPath = documentDirectoryPath.appending("/audios")
case .video:
subDirectoryPath = documentDirectoryPath.appending("/videos")
case .image:
subDirectoryPath = documentDirectoryPath.appending("/images")
case .document:
subDirectoryPath = documentDirectoryPath.appending("/documents")
}
print(subDirectoryPath)
let fileManager = FileManager.default
do {
let fileURLs = try fileManager.contentsOfDirectory(at: URL.init(string: subDirectoryPath)! , includingPropertiesForKeys: nil)
for file in fileURLs
{
if !file.absoluteString.contains("DS_Store")
{
var image = NSImage()
switch type
{
case .audio:
image = #imageLiteral(resourceName: "music-player")
case .video:
image = #imageLiteral(resourceName: "video-player")
case .document:
image = #imageLiteral(resourceName: "file")
case .image:
if let i = NSImage(contentsOf: file)
{
image = i
}
}
let name = String(describing: file).components(separatedBy: "/").last
let media = Media(image: image, url: file)
files[name!] = media
nothingFoundLabel.isHidden = true
}
}
if Array(files.keys).count < 1
{
nothingFoundLabel.isHidden = false
}
} catch {
nothingFoundLabel.isHidden = false
print("Error while enumerating files : \(error.localizedDescription)")
}
collectionView.reloadData()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment