Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anjor committed Oct 17, 2024
1 parent 79a2744 commit 644ed64
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions cmd-car-split.go
Original file line number Diff line number Diff line change
Expand Up @@ -655,21 +655,53 @@ func getFirstSlotFromURL(url string) (int64, error) {
return 0, fmt.Errorf("failed to parse Content-Length: %w", err)
}

// Make a GET request for the beginning of the file
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, fmt.Errorf("failed to create request: %w", err)
}

// Request only the first hdrSize bytes
req.Header.Set("Range", fmt.Sprintf("bytes=0-%d", hdrSize))

resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, fmt.Errorf("failed to fetch CAR file header: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusPartialContent {
return 0, fmt.Errorf("server does not support range requests")
}

// Read the header content
headerContent, err := io.ReadAll(resp.Body)
if err != nil {
return 0, fmt.Errorf("failed to read header content: %w", err)
}

// Parse the CAR header
rc := io.NopCloser(bytes.NewReader(headerContent))
cr, err := carreader.New(rc)
if err != nil {
return 0, fmt.Errorf("failed to create CarReader: %w", err)
}

// get the offset for the last section of the file
endOffset := fileSize - int64(maxSectionSize)
if endOffset < 0 {
endOffset = 0
}

// Now make the GET request with the Range header
req, err := http.NewRequest("GET", url, nil)
req, err = http.NewRequest("GET", url, nil)
if err != nil {
return 0, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Range", fmt.Sprintf("bytes=%d-", endOffset))

resp, err := http.DefaultClient.Do(req)
resp, err = http.DefaultClient.Do(req)
if err != nil {
return 0, fmt.Errorf("failed to fetch CAR file: %w", err)
}
Expand All @@ -686,13 +718,6 @@ func getFirstSlotFromURL(url string) (int64, error) {
return 0, fmt.Errorf("failed to read partial content: %w", err)
}

readCloser := io.NopCloser(bytes.NewReader(partialContent))

cr, err := carreader.New(readCloser)
if err != nil {
return 0, fmt.Errorf("failed to create CarReader: %w", err)
}

roots := cr.Header.Roots
if len(roots) != 1 {
return 0, fmt.Errorf("expected 1 root CID, got %d", len(roots))
Expand Down

0 comments on commit 644ed64

Please sign in to comment.