|
| 1 | +package utils |
| 2 | + |
| 3 | +import ( |
| 4 | + "EverythingSuckz/fsb/types" |
| 5 | + "context" |
| 6 | + "fmt" |
| 7 | + "io" |
| 8 | + "log" |
| 9 | + |
| 10 | + "github.com/gotd/td/telegram" |
| 11 | + "github.com/gotd/td/tg" |
| 12 | +) |
| 13 | + |
| 14 | +type linearReader struct { |
| 15 | + ctx context.Context |
| 16 | + parts []types.Part |
| 17 | + pos int |
| 18 | + client *telegram.Client |
| 19 | + next func() ([]byte, error) |
| 20 | + buffer []byte |
| 21 | + bytesread int64 |
| 22 | + chunkSize int64 |
| 23 | + i int64 |
| 24 | + contentLength int64 |
| 25 | +} |
| 26 | + |
| 27 | +func (*linearReader) Close() error { |
| 28 | + return nil |
| 29 | +} |
| 30 | + |
| 31 | +func NewLinearReader(ctx context.Context, client *telegram.Client, parts []types.Part, contentLength int64) (io.ReadCloser, error) { |
| 32 | + |
| 33 | + r := &linearReader{ |
| 34 | + ctx: ctx, |
| 35 | + parts: parts, |
| 36 | + client: client, |
| 37 | + chunkSize: int64(1024 * 1024), |
| 38 | + contentLength: contentLength, |
| 39 | + } |
| 40 | + |
| 41 | + r.next = r.partStream() |
| 42 | + |
| 43 | + return r, nil |
| 44 | +} |
| 45 | + |
| 46 | +func (r *linearReader) Read(p []byte) (n int, err error) { |
| 47 | + |
| 48 | + if r.bytesread == r.contentLength { |
| 49 | + log.Println("Linear Reader: EOF (bytesread == contentLength)") |
| 50 | + return 0, io.EOF |
| 51 | + } |
| 52 | + |
| 53 | + if r.i >= int64(len(r.buffer)) { |
| 54 | + r.buffer, err = r.next() |
| 55 | + if err != nil { |
| 56 | + return 0, err |
| 57 | + } |
| 58 | + if len(r.buffer) == 0 { |
| 59 | + r.pos++ |
| 60 | + if r.pos == len(r.parts) { |
| 61 | + log.Println("Linear Reader: EOF (pos==n(parts))") |
| 62 | + return 0, io.EOF |
| 63 | + } else { |
| 64 | + r.next = r.partStream() |
| 65 | + r.buffer, err = r.next() |
| 66 | + if err != nil { |
| 67 | + return 0, err |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + } |
| 72 | + r.i = 0 |
| 73 | + } |
| 74 | + |
| 75 | + n = copy(p, r.buffer[r.i:]) |
| 76 | + |
| 77 | + r.i += int64(n) |
| 78 | + |
| 79 | + r.bytesread += int64(n) |
| 80 | + |
| 81 | + return n, nil |
| 82 | +} |
| 83 | + |
| 84 | +func (r *linearReader) chunk(offset int64, limit int64) ([]byte, error) { |
| 85 | + |
| 86 | + req := &tg.UploadGetFileRequest{ |
| 87 | + Offset: offset, |
| 88 | + Limit: int(limit), |
| 89 | + Location: r.parts[r.pos].Location, |
| 90 | + } |
| 91 | + |
| 92 | + res, err := r.client.API().UploadGetFile(r.ctx, req) |
| 93 | + |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + switch result := res.(type) { |
| 99 | + case *tg.UploadFile: |
| 100 | + return result.Bytes, nil |
| 101 | + default: |
| 102 | + return nil, fmt.Errorf("unexpected type %T", r) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func (r *linearReader) partStream() func() ([]byte, error) { |
| 107 | + |
| 108 | + start := r.parts[r.pos].Start |
| 109 | + end := r.parts[r.pos].End |
| 110 | + offset := start - (start % r.chunkSize) |
| 111 | + |
| 112 | + firstPartCut := start - offset |
| 113 | + lastPartCut := (end % r.chunkSize) + 1 |
| 114 | + partCount := int((end - offset + r.chunkSize) / r.chunkSize) |
| 115 | + currentPart := 1 |
| 116 | + |
| 117 | + readData := func() ([]byte, error) { |
| 118 | + if currentPart > partCount { |
| 119 | + return make([]byte, 0), nil |
| 120 | + } |
| 121 | + res, err := r.chunk(offset, r.chunkSize) |
| 122 | + if err != nil { |
| 123 | + return nil, err |
| 124 | + } |
| 125 | + if len(res) == 0 { |
| 126 | + return res, nil |
| 127 | + } else if partCount == 1 { |
| 128 | + res = res[firstPartCut:lastPartCut] |
| 129 | + } else if currentPart == 1 { |
| 130 | + res = res[firstPartCut:] |
| 131 | + } else if currentPart == partCount { |
| 132 | + res = res[:lastPartCut] |
| 133 | + } |
| 134 | + |
| 135 | + currentPart++ |
| 136 | + offset += r.chunkSize |
| 137 | + return res, nil |
| 138 | + } |
| 139 | + return readData |
| 140 | +} |
0 commit comments