diff options
Diffstat (limited to 'rhimport/fetcher.go')
-rw-r--r-- | rhimport/fetcher.go | 68 |
1 files changed, 41 insertions, 27 deletions
diff --git a/rhimport/fetcher.go b/rhimport/fetcher.go index 2855ea0..e3280c4 100644 --- a/rhimport/fetcher.go +++ b/rhimport/fetcher.go @@ -556,36 +556,54 @@ func fetchFileDir(ctx *Context, res *Result, uri *url.URL, dir string, convert b return } -func fetchFileFake(ctx *Context, res *Result, uri *url.URL) error { - ctx.dbglog.Printf("Fake fetcher for '%s'", ctx.SourceUri) +func fetchFileSilence(ctx *Context, res *Result, uri *url.URL) error { + ctx.dbglog.Printf("Silence fetcher for '%s'", ctx.SourceUri) - duration, err := strconv.ParseUint(uri.Host, 10, 32) + d, err := strconv.ParseUint(uri.Host, 10, 32) if err != nil { res.ResponseCode = http.StatusBadRequest res.ErrorString = "invalid duration (must be a positive integer)" return nil } + duration := time.Duration(d) * 100 * time.Millisecond - for i := uint(0); i < uint(duration); i++ { - if ctx.isCanceled() { - ctx.stdlog.Printf("faking got canceled") - res.ResponseCode = http.StatusNoContent - res.ErrorString = "canceled" - return nil - } - - ctx.reportProgress(1, "faking", float64(i), float64(duration)) - time.Sleep(100 * time.Millisecond) + wav, err := NewPCMWavFile(uint32(ctx.conf.SampleRate), 16, uint16(ctx.Channels), duration) + if err != nil { + return err } - ctx.reportProgress(1, "faking", float64(duration), float64(duration)) + fileSize := wav.GetFileSize() + wav.Generator = NewSilenceGenerator() - ctx.SourceFile = "/nonexistend/fake.mp3" - ctx.OrigFilename = ctx.SourceFile - if ctx.SourceFilePolicy == Auto { - ctx.DeleteSourceFile = false - ctx.DeleteSourceDir = false - } - return nil + uri.Scheme = "attachment" + uri.Host = strconv.FormatUint(uint64(fileSize), 10) + uri.Path = "silence.wav" + + go func() { + for { + chunk := AttachmentChunk{} + var data [32 * 1024]byte + n, err := wav.Read(data[:]) + if n > 0 { + chunk.Data = data[:n] + if err == io.EOF { + err = nil + } + } + chunk.Error = err + if err == io.EOF { + return + } + + // TODO: handle cancel + ctx.AttachmentChan <- chunk + + if err != nil { + return + } + } + }() + + return fetchFileAttachment(ctx, res, uri) } func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv FetchConverter) error { @@ -633,11 +651,7 @@ func writeAttachmentFile(ctx *Context, res *Result, sizeTotal uint64, conv Fetch } func fetchFileAttachment(ctx *Context, res *Result, uri *url.URL) error { - ctx.dbglog.Printf("Attachment fetcher for '%s'", ctx.SourceUri) - - if ctx.AttachmentChan == nil { - return fmt.Errorf("attachement channel is nil") - } + ctx.dbglog.Printf("Attachment fetcher for '%s'", uri.String()) sizeTotal, err := strconv.ParseUint(uri.Host, 10, 64) if err != nil { @@ -701,7 +715,7 @@ var ( fetchers = map[string]FetchFunc{ "local": fetchFileLocal, "tmp": fetchFileTmp, - "fake": fetchFileFake, + "silence": fetchFileSilence, "attachment": fetchFileAttachment, } curlProtos = map[string]bool{ |