-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupload.js
144 lines (116 loc) · 4.24 KB
/
upload.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
fs = require("fs");
path = require("path");
AdmZip = require("adm-zip");
module.exports.convert = function (file) {
// Create a unique directory for each user
const BASE_OUTPUT_DIRECTORY = "output";
const uniqueID = Math.random().toString(36).substring(2, 15);
const OUTPUT_DIRECTORY = path.join(BASE_OUTPUT_DIRECTORY, uniqueID);
// Check if the file exists
if (!file) {
console.error("No file was uploaded");
// return an error http status code
return { status: 400, message: "No file was uploaded" };
}
// Check if the file is a JSON file
if (file.mimetype !== "application/json") {
console.error("The file is not a JSON file");
return { status: 400, message: "The file is not a JSON file" };
}
// Parse the file
let data = JSON.parse(file.data);
// Check if the file is empty
if (!data) {
console.error("The file is empty");
return { status: 400, message: "The file is empty" };
}
if (!data || typeof data !== "object") {
console.error(`Could not parse. Are you sure it's a JSON file?`);
return {
status: "400",
message: `Could not parse. Are you sure it's a JSON file?`,
};
}
if (!data.activeNotes || !Array.isArray(data.activeNotes)) {
console.error(
`There is no 'activeNotes' element in the data found. Are you sure it's a Simplenote file?`
);
return {
status: 400,
message: `There is no 'activeNotes' element in the data found. Are you sure it's a Simplenote file?`,
};
}
// Setup the output directory
if (!fs.existsSync(OUTPUT_DIRECTORY)) {
fs.mkdirSync(OUTPUT_DIRECTORY);
}
const filenames = {};
let noOfFiles = 0;
for (const note of data.activeNotes) {
const lines = note.content.split("\n");
if (lines.length === 0) {
console.log(`Skipping empty note with ID of ${note.id}`);
} else {
if (note.tags) {
const tags = note.tags.map((tag) => "#" + tag.replace(/\W+/g, "-"));
const tagText = tags.join(" ");
let tagPosition = "end";
if (tagPosition === "start") {
lines.splice(1, 0, "", tagText);
} else {
lines.push("", tagText);
}
}
// Function to sanitize filenames
function sanitizeFilename(filename) {
if (filename.length === 0) {
return Math.random().toString(36).substring(2, 15);
}
let NewfileName = filename
.replace(/[<>:"\/\\|?*\x00-\x1F]/g, "-")
.replace(/^CON$|^PRN$|^AUX$|^NUL$|^COM[1-9]$|^LPT[1-9]$/i, "_$&");
// If the NewfileName has a trailing '-' at the end, remove it
if (NewfileName.slice(-1) === "-") {
NewfileName = NewfileName.slice(0, -1);
}
return NewfileName;
}
// Assuming the rest of your code remains the same up to the filename assignment
let filename = sanitizeFilename(lines[0]);
if (filename.length > 88) {
filename = filename.substring(0, 88) + ".md";
} else {
filename = filename + ".md";
}
filename = filename.replace(/[/:]/g, ""); //.replace(/\s+/g, "-");
const filepath = path.join(OUTPUT_DIRECTORY, filename);
if (filenames[filename]) {
filenames[filename]++;
filename = `${filename.slice(0, -3)} ${filenames[filename]}.md`;
}
console.log(`Creating ${filename}...`);
noOfFiles++;
fs.writeFileSync(filepath, lines.join("\n"), "utf-8");
const creationTime = new Date(note.creationDate).getTime() / 1000;
console.log(note.creationDate);
console.log(creationTime);
fs.utimesSync(filepath, creationTime, creationTime);
const modifiedTime = new Date(note.lastModified).getTime() / 1000;
fs.utimesSync(filepath, modifiedTime, modifiedTime);
}
}
// ZIP up all the files and return the zip file
const zip = new AdmZip();
zip.addLocalFolder(OUTPUT_DIRECTORY);
zip.writeZip(`${OUTPUT_DIRECTORY}.zip`);
const numFiles = noOfFiles;
const filesWere = numFiles === 1 ? "file was" : "files were";
console.log(`\n${numFiles} .md ${filesWere} created in ${OUTPUT_DIRECTORY}`);
// Return a success message with the file name
let filename = file.name;
return {
status: 200,
message: `${filename} uploaded successfully`,
file: `${OUTPUT_DIRECTORY}.zip`,
};
};