github.com/goravel/framework@v1.13.9/filesystem/file.go (about)

     1  package filesystem
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"io"
     7  	"mime/multipart"
     8  	"os"
     9  	"path/filepath"
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/goravel/framework/contracts/config"
    14  	"github.com/goravel/framework/contracts/filesystem"
    15  	supportfile "github.com/goravel/framework/support/file"
    16  	"github.com/goravel/framework/support/str"
    17  )
    18  
    19  type File struct {
    20  	config  config.Config
    21  	disk    string
    22  	path    string
    23  	name    string
    24  	storage filesystem.Storage
    25  }
    26  
    27  func NewFile(file string) (*File, error) {
    28  	if !supportfile.Exists(file) {
    29  		return nil, errors.New("file doesn't exist")
    30  	}
    31  
    32  	return &File{
    33  		config:  ConfigFacade,
    34  		disk:    ConfigFacade.GetString("filesystems.default"),
    35  		path:    file,
    36  		name:    filepath.Base(file),
    37  		storage: StorageFacade,
    38  	}, nil
    39  }
    40  
    41  func NewFileFromRequest(fileHeader *multipart.FileHeader) (*File, error) {
    42  	src, err := fileHeader.Open()
    43  	if err != nil {
    44  		return nil, err
    45  	}
    46  	defer func(src multipart.File) {
    47  		if err = src.Close(); err != nil {
    48  			panic(err)
    49  		}
    50  	}(src)
    51  
    52  	tempFileName := fmt.Sprintf("%s_*%s", ConfigFacade.GetString("app.name"), filepath.Ext(fileHeader.Filename))
    53  	tempFile, err := os.CreateTemp(os.TempDir(), tempFileName)
    54  	if err != nil {
    55  		return nil, err
    56  	}
    57  	defer func(tempFile *os.File) {
    58  		if err = tempFile.Close(); err != nil {
    59  			panic(err)
    60  		}
    61  	}(tempFile)
    62  
    63  	_, err = io.Copy(tempFile, src)
    64  	if err != nil {
    65  		return nil, err
    66  	}
    67  
    68  	return &File{
    69  		config:  ConfigFacade,
    70  		disk:    ConfigFacade.GetString("filesystems.default"),
    71  		path:    tempFile.Name(),
    72  		name:    fileHeader.Filename,
    73  		storage: StorageFacade,
    74  	}, nil
    75  }
    76  
    77  func (f *File) Disk(disk string) filesystem.File {
    78  	f.disk = disk
    79  
    80  	return f
    81  }
    82  
    83  func (f *File) Extension() (string, error) {
    84  	return supportfile.Extension(f.path)
    85  }
    86  
    87  func (f *File) File() string {
    88  	return f.path
    89  }
    90  
    91  func (f *File) GetClientOriginalName() string {
    92  	return f.name
    93  }
    94  
    95  func (f *File) GetClientOriginalExtension() string {
    96  	return supportfile.ClientOriginalExtension(f.name)
    97  }
    98  
    99  func (f *File) HashName(path ...string) string {
   100  	var realPath string
   101  	if len(path) > 0 {
   102  		realPath = strings.TrimRight(path[0], "/") + "/"
   103  	}
   104  
   105  	extension, _ := supportfile.Extension(f.path, true)
   106  	if extension == "" {
   107  		return realPath + str.Random(40)
   108  	}
   109  
   110  	return realPath + str.Random(40) + "." + extension
   111  }
   112  
   113  func (f *File) LastModified() (time.Time, error) {
   114  	return supportfile.LastModified(f.path, f.config.GetString("app.timezone"))
   115  }
   116  
   117  func (f *File) MimeType() (string, error) {
   118  	return supportfile.MimeType(f.path)
   119  }
   120  
   121  func (f *File) Size() (int64, error) {
   122  	return supportfile.Size(f.path)
   123  }
   124  
   125  func (f *File) Store(path string) (string, error) {
   126  	return f.storage.Disk(f.disk).PutFile(path, f)
   127  }
   128  
   129  func (f *File) StoreAs(path string, name string) (string, error) {
   130  	return f.storage.Disk(f.disk).PutFileAs(path, f, name)
   131  }