-
Notifications
You must be signed in to change notification settings - Fork 2
/
changeTime.js
143 lines (119 loc) · 4.5 KB
/
changeTime.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
// ==Headers==
// @Name: changeTime
// @Description: changeTime
// @Version: 1.0.391
// @Author: dodying
// @Created: 2020-01-21 09:57:28
// @Modified: 2022-01-05 21:58:44
// @Namespace: https://github.com/dodying/Nodejs
// @SupportURL: https://github.com/dodying/Nodejs/issues
// @Require: fs-extra,jszip,readline-sync
// ==/Headers==
// usage: command []files
// command:
// 1. now
// 2. dl
// 3. old
// 修改文件时间 btime=上传时间 mtime=下载时间
// 修改文件夹时间 btime=最新一本的上传时间 mtime=最后检查(现在)/下载时间
// 空文件夹 远古
// 设置
// 导入原生模块
const path = require('path');
const cp = require('child_process');
// 导入第三方模块
const fse = require('fs-extra');
const JSZip = require('jszip');
// const readlineSync = require('readline-sync')
const walk = require('../../_lib/walk');
const timeFormat = require('../../_lib/timeFormat');
const parseInfo = require('../js/parseInfo');
// Function
const changeTime = async (file, btime, mtime) => {
try {
btime = timeFormat(btime);
mtime = timeFormat(mtime);
const info = fse.statSync(file);
console.log(file);
if (btime !== timeFormat(info.birthtimeMs)) {
console.log({ btime });
cp.execFileSync('powershell', [`(Get-Item -LiteralPath '${file}').CreationTime`, '=', '"', btime, '"']);
}
if (mtime !== timeFormat(info.mtimeMs)) {
console.log({ mtime });
cp.execFileSync('powershell', [`(Get-Item -LiteralPath '${file}').LastWriteTime`, '=', '"', mtime, '"']);
}
// readlineSync.keyInPause()
} catch (error) {
console.error(error);
}
// await waitInMs(20)
};
// Main
const main = async () => {
let [command, ...files] = process.argv.slice(2);
// console.log({ command, files })
files = files.map((file) => (fse.statSync(file).isDirectory() ? [file].concat(walk(file)) : [file]));
files = [].concat(...files).filter((item, index, array) => array.indexOf(item) === index);
const folders = files.filter((file) => fse.statSync(file).isDirectory()).sort((a, b) => (a.split('\\').length > b.split('\\').length ? -1 : a.split('\\').length === b.split('\\').length ? 0 : 1)).map((i) => i.replace(/\\+$/, ''));
files = files.filter((file) => fse.statSync(file).isFile() && ['.jpg', '.cbz'].includes(path.extname(file))).sort((a, b) => (path.extname(a) === '.cbz' ? -1 : 1));
for (const file of files) {
if (path.extname(file) === '.jpg') {
const info = path.parse(file);
const cbzFile = path.join(info.dir, `${info.name}.cbz`);
if (!fse.existsSync(cbzFile)) continue;
const cbzFileInfo = fse.statSync(cbzFile);
await changeTime(file, cbzFileInfo.birthtime, cbzFileInfo.mtime);
continue;
}
// 读取数据
const targetData = fse.readFileSync(file);
const jszip = new JSZip();
let zip;
try {
zip = await jszip.loadAsync(targetData);
} catch (error) {
console.error(`Error:\t无法读取文件 "${file}"`);
// readlineSync.keyInPause('Press any key to Continue')
continue;
}
// 查看列表
const fileList = Object.keys(zip.files);
// 检测有无info.txt
if (fileList.filter((item) => item.match(/(^|\/)info\.txt$/)).length === 0) {
console.warn('压缩档内不存在info.txt: ', file);
return new Error('no info.txt');
}
// 读取info.txt
const infoFile = fileList.find((item) => item.match(/(^|\/)info\.txt$/));
const data = await zip.files[infoFile].async('text');
const info = parseInfo(data);
await changeTime(file, info.Posted, info.downloadTime);
}
for (const folder of folders) {
const files = fse.readdirSync(folder).map((file) => path.join(folder, file)).filter((file) => ['.cbz'].includes(path.extname(file)));
if (files.length === 0) {
await changeTime(folder, '2010-01-01 00:00:00', '2010-01-01 00:00:00');
continue;
}
const infos = files.map((file) => fse.statSync(file));
const btime = infos.map((info) => info.birthtimeMs).sort().reverse()[0];
let mtime;
if (command === 'now') {
mtime = new Date();
} else if (command === 'dl') {
mtime = infos.map((info) => info.mtimeMs).sort().reverse()[0];
} else if (command === 'old') {
mtime = '2010-01-01 00:00:00';
} else {
mtime = btime;
}
await changeTime(folder, btime, mtime);
}
};
main().then(async () => {
//
}, async (err) => {
console.error(err);
process.exit(1);
});