-
Notifications
You must be signed in to change notification settings - Fork 238
/
folder.go
611 lines (524 loc) · 16.1 KB
/
folder.go
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
package pkg
import (
"fmt"
"os"
"path/filepath"
"runtime"
"time"
"github.com/ChineseSubFinder/ChineseSubFinder/pkg/get_access_time"
"github.com/sirupsen/logrus"
)
// --------------------------------------------------------------
// Debug
// --------------------------------------------------------------
// GetRootDebugFolder 在程序的根目录新建,调试用文件夹
func GetRootDebugFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, DebugFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetDebugFolderByName 根据传入的 strings (["aa", "bb"]) 组成 DebugFolder/aa/bb 这样的路径
func GetDebugFolderByName(names []string) (string, error) {
rootPath, err := GetRootDebugFolder()
if err != nil {
return "", err
}
tmpFolderFullPath := rootPath
for _, name := range names {
tmpFolderFullPath = filepath.Join(tmpFolderFullPath, name)
}
err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
if err != nil {
return "", err
}
return tmpFolderFullPath, nil
}
// CopyFiles2DebugFolder 把文件放入到 Debug 文件夹中,新建 desFolderName 文件夹
func CopyFiles2DebugFolder(names []string, subFiles []string) error {
debugFolderByName, err := GetDebugFolderByName(names)
if err != nil {
return err
}
// 复制下载在 tmp 文件夹中的字幕文件到视频文件夹下面
for _, subFile := range subFiles {
newFn := filepath.Join(debugFolderByName, filepath.Base(subFile))
err = CopyFile(subFile, newFn)
if err != nil {
return err
}
}
return nil
}
// --------------------------------------------------------------
// Tmp
// --------------------------------------------------------------
// GetRootTmpFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
func GetRootTmpFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, TmpFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetTmpFolderByName 获取缓存的文件夹,没有则新建
func GetTmpFolderByName(folderName string) (string, error) {
rootPath, err := GetRootTmpFolder()
if err != nil {
return "", err
}
tmpFolderFullPath := filepath.Join(rootPath, folderName)
err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
if err != nil {
return "", err
}
return tmpFolderFullPath, nil
}
// ClearTmpFolderByName 清理指定的缓存文件夹
func ClearTmpFolderByName(folderName string) error {
nowTmpFolder, err := GetTmpFolderByName(folderName)
if err != nil {
return err
}
return ClearFolder(nowTmpFolder)
}
// ClearRootTmpFolder 清理缓存的根目录,将里面的子文件夹一并清理
func ClearRootTmpFolder() error {
nowTmpFolder, err := GetRootTmpFolder()
if err != nil {
return err
}
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(nowTmpFolder)
if err != nil {
return err
}
for _, curFile := range files {
fullPath := nowTmpFolder + pathSep + curFile.Name()
if curFile.IsDir() {
err = os.RemoveAll(fullPath)
if err != nil {
return err
}
} else {
// 这里就是文件了
err = os.Remove(fullPath)
if err != nil {
return err
}
}
}
return nil
}
// --------------------------------------------------------------
// Adblock Cache
// --------------------------------------------------------------
// GetPluginRootFolder 在程序的根目录新建,取缓用文件夹,每一个视频的缓存将在其中额外新建子集文件夹
func GetPluginRootFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, PluginFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetPluginFolderByName 获取缓存的文件夹,没有则新建
func GetPluginFolderByName(folderName string) (string, error) {
rootPath, err := GetPluginRootFolder()
if err != nil {
return "", err
}
tmpFolderFullPath := filepath.Join(rootPath, folderName)
err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
if err != nil {
return "", err
}
return tmpFolderFullPath, nil
}
// ClearPluginFolderByName 清理指定的缓存文件夹
func ClearPluginFolderByName(folderName string) error {
nowTmpFolder, err := GetPluginFolderByName(folderName)
if err != nil {
return err
}
return ClearFolder(nowTmpFolder)
}
// --------------------------------------------------------------
// Rod Cache
// --------------------------------------------------------------
// GetRodTmpRootFolder 在程序的根目录新建,rod 缓存用文件夹
func GetRodTmpRootFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, RodCacheFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// ClearRodTmpRootFolder 清理 rod 缓存文件夹
func ClearRodTmpRootFolder() error {
nowTmpFolder, err := GetRodTmpRootFolder()
if err != nil {
return err
}
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(nowTmpFolder)
if err != nil {
return err
}
for _, curFile := range files {
fullPath := nowTmpFolder + pathSep + curFile.Name()
if curFile.IsDir() {
err = os.RemoveAll(fullPath)
if err != nil {
return err
}
} else {
// 这里就是文件了
err = os.Remove(fullPath)
if err != nil {
return err
}
}
}
return nil
}
// --------------------------------------------------------------
// Sub Fix Cache
// --------------------------------------------------------------
// GetRootSubFixCacheFolder 在程序的根目录新建,字幕时间校正的缓存文件夹
func GetRootSubFixCacheFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, SubFixCacheFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetRootCacheCenterFolder 下载缓存、队列缓存、下载次数缓存的文件夹
func GetRootCacheCenterFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, CacheCenterFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetSubFixCacheFolderByName 获取缓存的文件夹,没有则新建
func GetSubFixCacheFolderByName(folderName string) (string, error) {
rootPath, err := GetRootSubFixCacheFolder()
if err != nil {
return "", err
}
tmpFolderFullPath := filepath.Join(rootPath, folderName)
err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
if err != nil {
return "", err
}
return tmpFolderFullPath, nil
}
// --------------------------------------------------------------
// Share Sub Cache
// --------------------------------------------------------------
// GetShareSubRootFolder 在程序的根目录新建,字幕共享的缓存根目录,下级还有具体是按发行的时间去划分的子集目录
func GetShareSubRootFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, ShareSubFileCache)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// GetShareFolderByYear 缓存的文件夹以发行的年为一个单位存储
func GetShareFolderByYear(year int) (string, error) {
rootPath, err := GetShareSubRootFolder()
if err != nil {
return "", err
}
tmpFolderFullPath := filepath.Join(rootPath, fmt.Sprintf("%d", year))
err = os.MkdirAll(tmpFolderFullPath, os.ModePerm)
if err != nil {
return "", err
}
return tmpFolderFullPath, nil
}
// ClearShareSubFolderByYear 清理指定的缓存文件夹
func ClearShareSubFolderByYear(year int) error {
nowTmpFolder, err := GetShareFolderByYear(year)
if err != nil {
return err
}
return ClearFolder(nowTmpFolder)
}
// ClearShareSubFolderByYearAndName 清理指定的缓存文件夹
func ClearShareSubFolderByYearAndName(year int, name string) error {
nowTmpFolder, err := GetShareFolderByYear(year)
if err != nil {
return err
}
return ClearFolder(filepath.Join(nowTmpFolder, name))
}
// --------------------------------------------------------------
// Manual Subtitle Upload
// --------------------------------------------------------------
// GetManualSubUploadCacheFolder 手动上传字幕的缓存文件夹
func GetManualSubUploadCacheFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, ManualSubUploadCacheFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// ClearManualSubUploadCacheFolder 清理手动上传字幕的缓存文件夹
func ClearManualSubUploadCacheFolder() error {
nowTmpFolder, err := GetManualSubUploadCacheFolder()
if err != nil {
return err
}
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(nowTmpFolder)
if err != nil {
return err
}
for _, curFile := range files {
fullPath := nowTmpFolder + pathSep + curFile.Name()
if curFile.IsDir() {
err = os.RemoveAll(fullPath)
if err != nil {
return err
}
} else {
// 这里就是文件了
err = os.Remove(fullPath)
if err != nil {
return err
}
}
}
return nil
}
// --------------------------------------------------------------
// 视频和字幕的预览缓存
// --------------------------------------------------------------
// GetVideoAndSubPreviewCacheFolder 视频和字幕的预览缓存
func GetVideoAndSubPreviewCacheFolder() (string, error) {
nowProcessRoot, err := os.Getwd()
if err != nil {
return "", err
}
nowProcessRoot = filepath.Join(nowProcessRoot, cacheRootFolderName, VideoAndSubPreviewCacheFolder)
err = os.MkdirAll(nowProcessRoot, os.ModePerm)
if err != nil {
return "", err
}
return nowProcessRoot, err
}
// ClearVideoAndSubPreviewCacheFolder 清理视频和字幕的预览缓存
func ClearVideoAndSubPreviewCacheFolder() error {
nowTmpFolder, err := GetVideoAndSubPreviewCacheFolder()
if err != nil {
return err
}
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(nowTmpFolder)
if err != nil {
return err
}
for _, curFile := range files {
fullPath := nowTmpFolder + pathSep + curFile.Name()
if curFile.IsDir() {
err = os.RemoveAll(fullPath)
if err != nil {
return err
}
} else {
// 这里就是文件了
err = os.Remove(fullPath)
if err != nil {
return err
}
}
}
return nil
}
// --------------------------------------------------------------
// Common
// --------------------------------------------------------------
// ClearFolder 清空文件夹
func ClearFolder(folderFullPath string) error {
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(folderFullPath)
if err != nil {
return err
}
for _, curFile := range files {
fullPath := folderFullPath + pathSep + curFile.Name()
if curFile.IsDir() {
err = os.RemoveAll(fullPath)
if err != nil {
return err
}
} else {
// 这里就是文件了
err = os.Remove(fullPath)
if err != nil {
return err
}
}
}
return nil
}
// GetConfigRootDirFPath 获取 Config 的根目录,不同系统不一样
func GetConfigRootDirFPath() string {
nowConfigFPath := ""
sysType := runtime.GOOS
if sysType == "linux" {
if LinuxConfigPathInSelfPath() != "" {
// 自定义路径
nowConfigFPath = LinuxConfigPathInSelfPath()
} else {
// 专用目录,与 Docker 设置一致
nowConfigFPath = configDirRootFPathLinux
}
} else if sysType == "windows" {
if LinuxConfigPathInSelfPath() != "" {
// 自定义路径
nowConfigFPath = LinuxConfigPathInSelfPath()
} else {
nowConfigFPath = configDirRootFPathWindows
}
} else if sysType == "darwin" {
if LinuxConfigPathInSelfPath() != "" {
// 自定义路径
nowConfigFPath = LinuxConfigPathInSelfPath()
} else {
home, err := os.UserHomeDir()
if err != nil {
panic("GetConfigRootDirFPath darwin get UserHomeDir, Error:" + err.Error())
}
nowConfigFPath = home + configDirRootFPathDarwin
}
} else {
panic("GetConfigRootDirFPath can't matched OSType: " + sysType + " ,You Should Implement It Yourself")
}
return nowConfigFPath
}
// ClearIdleSubFixCacheFolder 清理闲置的字幕修正缓存文件夹
func ClearIdleSubFixCacheFolder(l *logrus.Logger, rootSubFixCacheFolder string, outOfDate time.Duration) error {
/*
从 GetRootSubFixCacheFolder 目录下,遍历第一级目录中的文件夹
然后每个文件夹中,统计里面最后的访问时间(可能有多个文件),如果超过某个时间范围就标记删除这个文件夹
*/
pathSep := string(os.PathSeparator)
files, err := os.ReadDir(rootSubFixCacheFolder)
if err != nil {
return err
}
wait2ScanFolder := make([]string, 0)
for _, curFile := range files {
fullPath := rootSubFixCacheFolder + pathSep + curFile.Name()
if curFile.IsDir() == true {
// 需要关注文件夹
wait2ScanFolder = append(wait2ScanFolder, fullPath)
}
}
wait2DeleteFolder := make([]string, 0)
getAccessTimeEx := get_access_time.GetAccessTimeEx{}
cutOff := time.Now().Add(-outOfDate)
for _, s := range wait2ScanFolder {
files, err = os.ReadDir(s)
if err != nil {
return err
}
maxAccessTime := time.Now()
// 需要统计这个文件夹下的所有文件的 AccessTIme,找出最新(最大的值)的那个时间,再比较
for i, curFile := range files {
fullPath := s + pathSep + curFile.Name()
if curFile.IsDir() == true {
continue
}
// 只需要关注文件
accessTime, err := getAccessTimeEx.GetAccessTime(fullPath)
if err != nil {
return err
}
if i == 0 {
maxAccessTime = accessTime
}
if Time2SecondNumber(accessTime) > Time2SecondNumber(maxAccessTime) {
maxAccessTime = accessTime
}
}
if maxAccessTime.Sub(cutOff) <= 0 {
// 确认可以删除
wait2DeleteFolder = append(wait2DeleteFolder, s)
}
}
// 统一清理过期的文件夹
for _, s := range wait2DeleteFolder {
l.Infoln("Try 2 clear SubFixCache Folder:", s)
err := os.RemoveAll(s)
if err != nil {
return err
}
}
return nil
}
// 缓存文件的位置信息,都是在程序的根目录下的 cache 中
const (
cacheRootFolderName = "cache" // 缓存文件夹总名称
TmpFolder = "tmp" // 临时缓存的文件夹
RodCacheFolder = "rod" // rod 的缓存目录
PluginFolder = "Plugin" // 插件的目录
DebugFolder = "CSF-DebugThings" // 调试相关的文件夹
SubFixCacheFolder = "CSF-SubFixCache" // 字幕时间校正的缓存文件夹,一般可以不清理
ShareSubFileCache = "CSF-ShareSubCache" // 字幕共享的缓存目录,不建议删除
CacheCenterFolder = "CSF-CacheCenter" // 下载缓存、队列缓存、下载次数缓存的文件夹
ManualSubUploadCacheFolder = "CSF-ManualSubUploadCache" // 手动上传字幕的缓存文件夹
VideoAndSubPreviewCacheFolder = "CSF-VideoAndSubPreviewCache" // 视频和字幕的预览缓存
)
const (
Plugin_Adblock = "adblock"
)
// 配置文件的位置信息,这个会根据系统版本做区分
const (
configDirRootFPathWindows = "." // Windows 就是在当前的程序目录
configDirRootFPathLinux = "/config" // Linux 是在 /config 下
configDirRootFPathDarwin = "/.config/chinesesubfinder" // Darwin 是在 os.UserHomeDir()/.config/chinesesubfinder/ 下
)