mirror of
https://git.sr.ht/~phw/scotty
synced 2025-06-01 19:38:34 +02:00
This will allow cancelling the export if the import fails before the export finished. For now the context isn't passed on to the actual export functions, hence there is not yet any cancellation happening.
141 lines
4.3 KiB
Go
141 lines
4.3 KiB
Go
/*
|
|
Copyright © 2023-2025 Philipp Wolfer <phw@uploadedlobster.com>
|
|
|
|
This file is part of Scotty.
|
|
|
|
Scotty is free software: you can redistribute it and/or modify it under the
|
|
terms of the GNU General Public License as published by the Free Software
|
|
Foundation, either version 3 of the License, or (at your option) any later version.
|
|
|
|
Scotty is distributed in the hope that it will be useful, but WITHOUT ANY
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
|
Scotty. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
package backends
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
|
|
"go.uploadedlobster.com/scotty/internal/models"
|
|
)
|
|
|
|
type ImportProcessor[T models.ListensResult | models.LovesResult] interface {
|
|
ImportBackend() models.ImportBackend
|
|
Process(ctx context.Context, wg *sync.WaitGroup, results chan T, out chan models.ImportResult, progress chan models.TransferProgress)
|
|
Import(export T, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error)
|
|
}
|
|
|
|
type ListensImportProcessor struct {
|
|
Backend models.ListensImport
|
|
}
|
|
|
|
func (p ListensImportProcessor) ImportBackend() models.ImportBackend {
|
|
return p.Backend
|
|
}
|
|
|
|
func (p ListensImportProcessor) Process(ctx context.Context, wg *sync.WaitGroup, results chan models.ListensResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
|
process(ctx, wg, p, results, out, progress)
|
|
}
|
|
|
|
func (p ListensImportProcessor) Import(export models.ListensResult, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error) {
|
|
if export.Error != nil {
|
|
return result, export.Error
|
|
}
|
|
|
|
if export.Total > 0 {
|
|
result.TotalCount = export.Total
|
|
} else {
|
|
result.TotalCount += len(export.Items)
|
|
}
|
|
importResult, err := p.Backend.ImportListens(export, result, progress)
|
|
if err != nil {
|
|
return importResult, err
|
|
}
|
|
return importResult, nil
|
|
}
|
|
|
|
type LovesImportProcessor struct {
|
|
Backend models.LovesImport
|
|
}
|
|
|
|
func (p LovesImportProcessor) ImportBackend() models.ImportBackend {
|
|
return p.Backend
|
|
}
|
|
|
|
func (p LovesImportProcessor) Process(ctx context.Context, wg *sync.WaitGroup, results chan models.LovesResult, out chan models.ImportResult, progress chan models.TransferProgress) {
|
|
process(ctx, wg, p, results, out, progress)
|
|
}
|
|
|
|
func (p LovesImportProcessor) Import(export models.LovesResult, result models.ImportResult, out chan models.ImportResult, progress chan models.TransferProgress) (models.ImportResult, error) {
|
|
if export.Error != nil {
|
|
return result, export.Error
|
|
}
|
|
|
|
if export.Total > 0 {
|
|
result.TotalCount = export.Total
|
|
} else {
|
|
result.TotalCount += len(export.Items)
|
|
}
|
|
importResult, err := p.Backend.ImportLoves(export, result, progress)
|
|
if err != nil {
|
|
return importResult, err
|
|
}
|
|
return importResult, nil
|
|
}
|
|
|
|
func process[R models.LovesResult | models.ListensResult, P ImportProcessor[R]](
|
|
ctx context.Context, wg *sync.WaitGroup,
|
|
processor P, results chan R,
|
|
out chan models.ImportResult,
|
|
progress chan models.TransferProgress,
|
|
) {
|
|
wg.Add(1)
|
|
defer wg.Done()
|
|
defer close(out)
|
|
result := models.ImportResult{}
|
|
p := models.TransferProgress{}
|
|
|
|
if err := processor.ImportBackend().StartImport(); err != nil {
|
|
out <- handleError(result, err, progress)
|
|
return
|
|
}
|
|
|
|
for exportResult := range results {
|
|
select {
|
|
case <-ctx.Done():
|
|
processor.ImportBackend().FinishImport()
|
|
out <- handleError(result, ctx.Err(), progress)
|
|
return
|
|
default:
|
|
importResult, err := processor.Import(exportResult, result, out, progress)
|
|
result.Update(importResult)
|
|
if err != nil {
|
|
processor.ImportBackend().FinishImport()
|
|
out <- handleError(result, err, progress)
|
|
return
|
|
}
|
|
progress <- p.FromImportResult(result, false)
|
|
}
|
|
}
|
|
|
|
if err := processor.ImportBackend().FinishImport(); err != nil {
|
|
out <- handleError(result, err, progress)
|
|
return
|
|
}
|
|
|
|
progress <- p.FromImportResult(result, true)
|
|
out <- result
|
|
}
|
|
|
|
func handleError(result models.ImportResult, err error, progress chan models.TransferProgress) models.ImportResult {
|
|
result.Error = err
|
|
p := models.TransferProgress{}.FromImportResult(result, false)
|
|
p.Import.Abort()
|
|
progress <- p
|
|
return result
|
|
}
|