code.gitea.io/gitea@v1.22.3/modules/git/blob.go (about)

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