github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/git/blob.go (about)

     1  // Copyright 2023 The GitBundle Inc. All rights reserved.
     2  // Copyright 2017 The Gitea Authors. All rights reserved.
     3  // Use of this source code is governed by a MIT-style
     4  // license that can be found in the LICENSE file.
     5  
     6  // Copyright 2015 The Gogs Authors. All rights reserved.
     7  
     8  package git
     9  
    10  import (
    11  	"bytes"
    12  	"encoding/base64"
    13  	"io"
    14  
    15  	"github.com/gitbundle/modules/typesniffer"
    16  	"github.com/gitbundle/modules/util"
    17  )
    18  
    19  // This file contains common functions between the gogit and !gogit variants for git Blobs
    20  
    21  // Name returns name of the tree entry this blob object was created from (or empty string)
    22  func (b *Blob) Name() string {
    23  	return b.name
    24  }
    25  
    26  // GetBlobContent Gets the content of the blob as raw text
    27  func (b *Blob) GetBlobContent() (string, error) {
    28  	dataRc, err := b.DataAsync()
    29  	if err != nil {
    30  		return "", err
    31  	}
    32  	defer dataRc.Close()
    33  	buf := make([]byte, 1024)
    34  	n, _ := util.ReadAtMost(dataRc, buf)
    35  	buf = buf[:n]
    36  	return string(buf), nil
    37  }
    38  
    39  // GetBlobLineCount gets line count of the blob
    40  func (b *Blob) GetBlobLineCount() (int, error) {
    41  	reader, err := b.DataAsync()
    42  	if err != nil {
    43  		return 0, err
    44  	}
    45  	defer reader.Close()
    46  	buf := make([]byte, 32*1024)
    47  	count := 1
    48  	lineSep := []byte{'\n'}
    49  
    50  	c, err := reader.Read(buf)
    51  	if c == 0 && err == io.EOF {
    52  		return 0, nil
    53  	}
    54  	for {
    55  		count += bytes.Count(buf[:c], lineSep)
    56  		switch {
    57  		case err == io.EOF:
    58  			return count, nil
    59  		case err != nil:
    60  			return count, err
    61  		}
    62  		c, err = reader.Read(buf)
    63  	}
    64  }
    65  
    66  // GetBlobContentBase64 Reads the content of the blob with a base64 encode and returns the encoded string
    67  func (b *Blob) GetBlobContentBase64() (string, error) {
    68  	dataRc, err := b.DataAsync()
    69  	if err != nil {
    70  		return "", err
    71  	}
    72  	defer dataRc.Close()
    73  
    74  	pr, pw := io.Pipe()
    75  	encoder := base64.NewEncoder(base64.StdEncoding, pw)
    76  
    77  	go func() {
    78  		_, err := io.Copy(encoder, dataRc)
    79  		_ = encoder.Close()
    80  
    81  		if err != nil {
    82  			_ = pw.CloseWithError(err)
    83  		} else {
    84  			_ = pw.Close()
    85  		}
    86  	}()
    87  
    88  	out, err := io.ReadAll(pr)
    89  	if err != nil {
    90  		return "", err
    91  	}
    92  	return string(out), nil
    93  }
    94  
    95  // GuessContentType guesses the content type of the blob.
    96  func (b *Blob) GuessContentType() (typesniffer.SniffedType, error) {
    97  	r, err := b.DataAsync()
    98  	if err != nil {
    99  		return typesniffer.SniffedType{}, err
   100  	}
   101  	defer r.Close()
   102  
   103  	return typesniffer.DetectContentTypeFromReader(r)
   104  }