Skip to content

Instantly share code, notes, and snippets.

@arkadijs
Last active May 5, 2023 14:34
Show Gist options
  • Select an option

  • Save arkadijs/172d9bda4e0d10a530add1faef942852 to your computer and use it in GitHub Desktop.

Select an option

Save arkadijs/172d9bda4e0d10a530add1faef942852 to your computer and use it in GitHub Desktop.

Revisions

  1. arkadijs revised this gist May 5, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pipeline.go
    Original file line number Diff line number Diff line change
    @@ -25,7 +25,7 @@ func overlap(start, end, timestamp int, bytes []byte) bool {
    func NewAnonymizator(start, end int, next Writer) Writer {
    anonymize := func(timestamp int, bytes []byte) {
    fmt.Printf("anon start=%d end=%d: ts=%02d endTs=%02d %s\n",
    start, end, timestamp, timestamp+len(bytes)/rate, bytes)
    start, end, timestamp, timestamp+len(bytes)/rate-1, bytes)
    if overlap(start, end, timestamp, bytes) {
    // proper slice split logic instead
    for i := range bytes {
  2. arkadijs revised this gist May 4, 2023. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions pipeline.go
    Original file line number Diff line number Diff line change
    @@ -22,7 +22,7 @@ func overlap(start, end, timestamp int, bytes []byte) bool {
    (end >= timestamp && end <= endTimestamp)
    }

    func NewAnonimizator(start, end int, next Writer) Writer {
    func NewAnonymizator(start, end int, next Writer) Writer {
    anonymize := func(timestamp int, bytes []byte) {
    fmt.Printf("anon start=%d end=%d: ts=%02d endTs=%02d %s\n",
    start, end, timestamp, timestamp+len(bytes)/rate, bytes)
    @@ -129,7 +129,7 @@ func main() {
    pipeline := NewEncoder(NewUploader())
    // anonymization doesn't care if it is applied in reverse order
    for _, a := range anon {
    pipeline = NewAnonimizator(a.Start, a.End, pipeline)
    pipeline = NewAnonymizator(a.Start, a.End, pipeline)
    }

    writer := NewIoWriterAdapter(pipeline)
  3. arkadijs revised this gist May 4, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion pipeline.go
    Original file line number Diff line number Diff line change
    @@ -127,7 +127,7 @@ func main() {
    }

    pipeline := NewEncoder(NewUploader())
    // anonimization doesn't care if it is applied in reverse order
    // anonymization doesn't care if it is applied in reverse order
    for _, a := range anon {
    pipeline = NewAnonimizator(a.Start, a.End, pipeline)
    }
  4. arkadijs revised this gist May 4, 2023. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion pipeline.go
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,8 @@ func overlap(start, end, timestamp int, bytes []byte) bool {

    func NewAnonimizator(start, end int, next Writer) Writer {
    anonymize := func(timestamp int, bytes []byte) {
    fmt.Printf("anon start=%d end=%d: ts=%02d endTs=%02d %s\n", start, end, timestamp, timestamp+len(bytes)/rate, bytes)
    fmt.Printf("anon start=%d end=%d: ts=%02d endTs=%02d %s\n",
    start, end, timestamp, timestamp+len(bytes)/rate, bytes)
    if overlap(start, end, timestamp, bytes) {
    // proper slice split logic instead
    for i := range bytes {
  5. arkadijs created this gist May 4, 2023.
    137 changes: 137 additions & 0 deletions pipeline.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,137 @@
    package main

    import (
    "fmt"
    "io"
    "strings"
    )

    const rate = 2 // bytes per millisec of "audio"

    type Anon struct {
    Start, End int
    }

    type Writer func(timestamp int, bytes []byte)

    func overlap(start, end, timestamp int, bytes []byte) bool {
    endTimestamp := timestamp + len(bytes)/rate
    return (timestamp >= start && timestamp <= end) ||
    (endTimestamp >= start && endTimestamp <= end) ||
    (start >= timestamp && start <= endTimestamp) ||
    (end >= timestamp && end <= endTimestamp)
    }

    func NewAnonimizator(start, end int, next Writer) Writer {
    anonymize := func(timestamp int, bytes []byte) {
    fmt.Printf("anon start=%d end=%d: ts=%02d endTs=%02d %s\n", start, end, timestamp, timestamp+len(bytes)/rate, bytes)
    if overlap(start, end, timestamp, bytes) {
    // proper slice split logic instead
    for i := range bytes {
    currentTimestamp := timestamp + i/rate
    if currentTimestamp >= start && currentTimestamp <= end {
    bytes[i] = '_'
    }
    }
    // ...
    }
    next(timestamp, bytes)
    }

    return anonymize
    }

    func NewEncoder(next Writer) Writer {
    passthru := func(timestamp int, bytes []byte) {
    next(timestamp, bytes)
    }

    return passthru
    }

    func upload(body io.Reader) {
    buf := make([]byte, 5)
    for {
    n, err := body.Read(buf)
    if n > 0 {
    fmt.Printf("upload: len=%d %s\n", n, buf[:n])
    }
    if err != nil {
    break
    }
    }
    }

    type WriterReaderBridge struct {
    ch chan []byte
    tail []byte
    }

    func NewWriterReaderBridge(ch chan []byte) *WriterReaderBridge {
    return &WriterReaderBridge{ch, nil}
    }

    func (bridge *WriterReaderBridge) Read(out []byte) (int, error) {
    if len(bridge.tail) > 0 {
    read := copy(out, bridge.tail)
    if read >= len(bridge.tail) {
    bridge.tail = nil
    } else {
    bridge.tail = bridge.tail[read:]
    }
    return read, nil
    }

    chunk := <-bridge.ch
    read := copy(out, chunk)
    if read < len(chunk) {
    bridge.tail = chunk[read:]
    }

    return read, nil
    }

    func NewUploader() Writer {
    ch := make(chan []byte)

    writer := func(_ int, bytes []byte) {
    ch <- bytes
    }

    go upload(NewWriterReaderBridge(ch))

    return writer
    }

    type IoWriterAdapter struct {
    writer Writer
    timestamp int
    }

    func (adapter *IoWriterAdapter) Write(p []byte) (n int, err error) {
    adapter.writer(adapter.timestamp, p)
    adapter.timestamp += len(p) / rate

    return len(p), nil
    }

    func NewIoWriterAdapter(writer Writer) *IoWriterAdapter {
    return &IoWriterAdapter{writer, 0}
    }

    func main() {
    anon := []Anon{
    {2, 3},
    {7, 9},
    }

    pipeline := NewEncoder(NewUploader())
    // anonimization doesn't care if it is applied in reverse order
    for _, a := range anon {
    pipeline = NewAnonimizator(a.Start, a.End, pipeline)
    }

    writer := NewIoWriterAdapter(pipeline)
    io.Copy(writer, strings.NewReader("0123456789"))
    io.Copy(writer, strings.NewReader("abcdefghijklmnopqrstxyz"))
    }