-
Notifications
You must be signed in to change notification settings - Fork 69
Expand file tree
/
Copy pathcode_intel_upload_transfer.go
More file actions
82 lines (68 loc) · 1.97 KB
/
code_intel_upload_transfer.go
File metadata and controls
82 lines (68 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"context"
"os"
"github.com/sourcegraph/sourcegraph/lib/codeintel/upload"
"github.com/sourcegraph/sourcegraph/lib/output"
)
func UploadUncompressedIndex(ctx context.Context, filename string, httpClient upload.Client, opts upload.UploadOptions) (int, error) {
originalReader, originalSize, err := openFileAndGetSize(filename)
if err != nil {
return 0, err
}
defer func() {
_ = originalReader.Close()
}()
bars := []output.ProgressBar{{Label: "Compressing", Max: 1.0}}
progress, _, cleanup := logProgress(
opts.Output,
bars,
"Index compressed",
"Failed to compress index",
)
compressedFile, err := compressReaderToDisk(originalReader, originalSize, progress)
if err != nil {
cleanup(err)
return 0, err
}
defer func() {
_ = os.Remove(compressedFile)
}()
compressedSize, err := getFileSize(compressedFile)
if err != nil {
cleanup(err)
return 0, err
}
cleanup(nil)
if opts.Output != nil {
opts.Output.WriteLine(output.Linef(
output.EmojiLightbulb,
output.StyleItalic,
"Indexed compressed (%.2fMB -> %.2fMB).",
float64(originalSize)/1000/1000,
float64(compressedSize)/1000/1000,
))
}
return UploadCompressedIndex(ctx, compressedFile, httpClient, opts, originalSize)
}
func UploadCompressedIndex(ctx context.Context, compressedFile string, httpClient upload.Client, opts upload.UploadOptions, uncompressedSize int64) (int, error) {
compressedReader, compressedSize, err := openFileAndGetSize(compressedFile)
if err != nil {
// cleanup(err)
return 0, err
}
defer func() {
_ = compressedReader.Close()
}()
if compressedSize <= opts.MaxPayloadSizeBytes {
return uploadIndex(ctx, httpClient, opts, compressedReader, compressedSize, uncompressedSize)
}
return uploadMultipartIndex(ctx, httpClient, opts, compressedReader, compressedSize, uncompressedSize)
}
func getFileSize(filename string) (int64, error) {
fileInfo, err := os.Stat(filename)
if err != nil {
return 1, err
}
return fileInfo.Size(), nil
}