-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscanLibrary.js
125 lines (108 loc) · 3.34 KB
/
scanLibrary.js
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import path from "path";
import fs from "fs";
// Helper function to parse dates from filenames
const fileNameToDate = (string, includesTime) => {
const part1 = string.substring(0, 10);
const part2 = includesTime
? string.substring(11, 19).replace(/-/g, ":")
: "12:00:00";
const fullDateString = `${part1}T${part2}.000-04:00`; // Shift Eastern timestamp to GMT
const dateObj = new Date(fullDateString);
return dateObj;
};
// Main function to scan library
const scanLibrary = (pathString) => {
const exclusions = [".DS_Store", ".gitkeep"];
const assetPath = path.resolve(process.cwd(), pathString);
const albumDirs = fs.readdirSync(assetPath, "utf-8");
const albumSlugs = albumDirs.filter(
(dir) => !exclusions.some((term) => dir.includes(term))
);
const assetLibrary = albumSlugs.map((slug) => {
const albumHidden = slug.includes("[hidden]");
const albumContentsPath = path.resolve(
process.cwd(),
`${pathString}/${slug}`
);
const albumContents = fs.readdirSync(albumContentsPath, "utf-8");
let albumName = slug
.substring(11)
.replace(/\[hidden\]/g, "")
.replace(/-/g, " ");
let urlSlug = albumHidden ? slug.replace("-[hidden]", "") : slug;
const albumDate = fileNameToDate(slug, false).toISOString();
const albumYear = albumDate.slice(0, 4);
const files = albumContents
.filter((fileName) => !exclusions.some((term) => fileName.includes(term)))
.map((fileName) => {
const filePath = `${albumContentsPath}/${fileName}`;
const extRaw = path.extname(fileName);
const extension = extRaw.toLowerCase().replace(".", "");
const fileBase = path.basename(fileName, extRaw);
const fileID = fileName.replace(/\./g, "-");
const fileDate = fileNameToDate(fileBase, true);
let fileDateString = null;
if (fileDate) {
fileDateString = fileDate.toLocaleDateString("en-US", {
month: "long",
day: "numeric",
year: "numeric",
});
}
let fileType = null;
switch (extension) {
case "txt":
case "md":
fileType = "text";
break;
case "mov":
case "mp4":
fileType = "video";
break;
case "jpeg":
case "jpg":
fileType = "image";
break;
default:
fileType = null;
}
let textHeading = null;
let textContents = null;
if (["txt", "md"].includes(extension)) {
textHeading = fileName
.substring(20)
.replace(/-/g, " ")
.replace(`.${extension}`, "");
textContents = fs
.readFileSync(filePath)
.toString()
.replace(/(?:\r\n|\r|\n)/g, "<br />");
}
return {
fileBase,
fileName,
fileID,
fileDateString,
extension,
filePath,
fileType,
textHeading,
textContents,
};
});
const firstImage = files.find((file) => file.fileType == "image");
return {
albumDate,
albumHidden,
albumName,
albumYear,
files,
firstImage,
slug,
urlSlug,
};
});
return assetLibrary.reverse();
};
// Export the function for ES module
export default scanLibrary;