tiktok-dl/utils/archive.go

56 lines
1.4 KiB
Go
Raw Normal View History

2020-03-22 00:10:24 +00:00
package utils
import (
2020-04-12 01:22:00 +01:00
models "github.com/pikami/tiktok-dl/models"
config "github.com/pikami/tiktok-dl/models/config"
res "github.com/pikami/tiktok-dl/resources"
2020-04-12 01:22:00 +01:00
fileio "github.com/pikami/tiktok-dl/utils/fileio"
log "github.com/pikami/tiktok-dl/utils/log"
2020-03-22 00:10:24 +00:00
)
// IsItemInArchive - Checks if the item is already archived
func IsItemInArchive(upload models.Upload) bool {
if len(RemoveArchivedItems([]models.Upload{upload})) == 0 {
return true
}
return false
}
// RemoveArchivedItems - Returns items slice without archived items
func RemoveArchivedItems(uploads []models.Upload) []models.Upload {
archiveFilePath := config.Config.ArchiveFilePath
2020-03-21 22:22:08 +00:00
if archiveFilePath == "" || !fileio.CheckIfExists(archiveFilePath) {
2020-03-22 00:10:24 +00:00
return uploads
}
removeArchivedItemsDelegate := func(archivedItem string) {
for i, upload := range uploads {
if upload.GetUploadID() == archivedItem {
uploads = append(uploads[:i], uploads[i+1:]...)
}
}
}
lenBeforeRemoval := len(uploads)
2020-03-21 22:22:08 +00:00
fileio.ReadFileLineByLine(archiveFilePath, removeArchivedItemsDelegate)
2020-03-22 00:10:24 +00:00
removedCount := lenBeforeRemoval - len(uploads)
if removedCount > 0 {
log.Logf(res.ItemsFoundInArchive, removedCount)
2020-03-22 00:10:24 +00:00
}
return uploads
}
// AddItemToArchive - Adds item to archived list
func AddItemToArchive(uploadID string) {
archiveFilePath := config.Config.ArchiveFilePath
if archiveFilePath == "" {
return
}
2020-03-21 22:22:08 +00:00
fileio.AppendToFile(uploadID, archiveFilePath)
2020-03-22 00:10:24 +00:00
}