-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmanager.hs
More file actions
52 lines (49 loc) · 2.82 KB
/
manager.hs
File metadata and controls
52 lines (49 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module Manager (
processDirectory
) where
import System.Directory
import System.FilePath
import Photo
import Data.Time
import System.Locale
-- возвращает директорию с фотографиями, считывая ее путь из файла настроек
picturesDir :: IO FilePath
picturesDir =
getAppUserDataDirectory "photosort" >>= createIfNull >>= (readDefault "Pictures" . (`combine` "pictures")) >>= return
where
--создаем директорию, если нужно
createIfNull appDir = createDirectoryIfMissing True appDir >> (return appDir)
--проверяем, существует ли данный файл
readDefault def fileName = doesFileExist fileName >>= (readSafe fileName def)
--если не существует, создаем его и пишем туда дефолтное значение
readSafe fileName def False = do
home <- getHomeDirectory
let fullPath = home </> def
writeFile fileName fullPath
return fullPath
--иначе просто читаем значение
readSafe fileName _ True = readFile fileName >>= return
--рекурсивная обработка всех каталогов
processDirectory :: FilePath -> IO ()
processDirectory dir =
getDirectoryContents dir >>= checkItems . filter ( ((/=) '.') . head ) >>= (mapM_ processSingle)
where
--по заданному списку содержимого каталога возвращаем кортежи с маркером "каталог" для каждого элемента
checkItems xs = mapM (singleCheck . (dir </>)) xs
where
singleCheck path = (doesDirectoryExist path) >>= (\isDirectory -> return (path, isDirectory))
--обработка элемента: если каталог - входим в рекурсию, иначе обрабатываем фотографию
processSingle (path, True) = processDirectory path
processSingle (path, False) = do
pictures <- picturesDir
--ловим экзепшн, ведь возможно этот файл вовсе не фотография
maybeDate <- catch (getTime path) nothing
copyPhoto pictures maybeDate
where
nothing x = return Nothing
-- безопасное копирование
copyPhoto pictures Nothing = return ()
copyPhoto pictures (Just date) = do
let newPath = addTrailingPathSeparator $ pictures </> (formatTime defaultTimeLocale "%Y/%B/%d" date)
createDirectoryIfMissing True newPath
copyFile path (newPath </> (takeFileName path))