Skip to content

Commit

Permalink
fix: path error in Windows (#192)
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi authored Jun 19, 2023
1 parent 891abfb commit 87db2a7
Show file tree
Hide file tree
Showing 14 changed files with 92 additions and 80 deletions.
20 changes: 10 additions & 10 deletions auth/console/jwt_secret_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package console

import (
"errors"
"io/ioutil"
"os"
"strings"

"github.com/gookit/color"
Expand All @@ -21,24 +21,24 @@ func NewJwtSecretCommand(config config.Config) *JwtSecretCommand {
return &JwtSecretCommand{config: config}
}

//Signature The name and signature of the console command.
// Signature The name and signature of the console command.
func (receiver *JwtSecretCommand) Signature() string {
return "jwt:secret"
}

//Description The console command description.
// Description The console command description.
func (receiver *JwtSecretCommand) Description() string {
return "Set the JWTAuth secret key used to sign the tokens"
}

//Extend The console command extend.
// Extend The console command extend.
func (receiver *JwtSecretCommand) Extend() command.Extend {
return command.Extend{
Category: "jwt",
}
}

//Handle Execute the console command.
// Handle Execute the console command.
func (receiver *JwtSecretCommand) Handle(ctx console.Context) error {
key := receiver.generateRandomKey()

Expand All @@ -53,12 +53,12 @@ func (receiver *JwtSecretCommand) Handle(ctx console.Context) error {
return nil
}

//generateRandomKey Generate a random key for the application.
// generateRandomKey Generate a random key for the application.
func (receiver *JwtSecretCommand) generateRandomKey() string {
return str.Random(32)
}

//setSecretInEnvironmentFile Set the application key in the environment file.
// setSecretInEnvironmentFile Set the application key in the environment file.
func (receiver *JwtSecretCommand) setSecretInEnvironmentFile(key string) error {
currentKey := receiver.config.GetString("jwt.secret")

Expand All @@ -75,16 +75,16 @@ func (receiver *JwtSecretCommand) setSecretInEnvironmentFile(key string) error {
return nil
}

//writeNewEnvironmentFileWith Write a new environment file with the given key.
// writeNewEnvironmentFileWith Write a new environment file with the given key.
func (receiver *JwtSecretCommand) writeNewEnvironmentFileWith(key string) error {
content, err := ioutil.ReadFile(".env")
content, err := os.ReadFile(".env")
if err != nil {
return err
}

newContent := strings.Replace(string(content), "JWT_SECRET="+receiver.config.GetString("jwt.secret"), "JWT_SECRET="+key, 1)

err = ioutil.WriteFile(".env", []byte(newContent), 0644)
err = os.WriteFile(".env", []byte(newContent), 0644)
if err != nil {
return err
}
Expand Down
18 changes: 9 additions & 9 deletions console/console/key_generate_command.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package console

import (
"io/ioutil"
"os"
"strings"

"github.com/gookit/color"
Expand All @@ -23,24 +23,24 @@ func NewKeyGenerateCommand(config config.Config) *KeyGenerateCommand {
}
}

//Signature The name and signature of the console command.
// Signature The name and signature of the console command.
func (receiver *KeyGenerateCommand) Signature() string {
return "key:generate"
}

//Description The console command description.
// Description The console command description.
func (receiver *KeyGenerateCommand) Description() string {
return "Set the application key"
}

//Extend The console command extend.
// Extend The console command extend.
func (receiver *KeyGenerateCommand) Extend() command.Extend {
return command.Extend{
Category: "key",
}
}

//Handle Execute the console command.
// Handle Execute the console command.
func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error {
if receiver.config.GetString("app.env") == "production" {
color.Yellowln("**************************************")
Expand Down Expand Up @@ -74,21 +74,21 @@ func (receiver *KeyGenerateCommand) Handle(ctx console.Context) error {
return nil
}

//generateRandomKey Generate a random key for the application.
// generateRandomKey Generate a random key for the application.
func (receiver *KeyGenerateCommand) generateRandomKey() string {
return str.Random(32)
}

//writeNewEnvironmentFileWith Write a new environment file with the given key.
// writeNewEnvironmentFileWith Write a new environment file with the given key.
func (receiver *KeyGenerateCommand) writeNewEnvironmentFileWith(key string) error {
content, err := ioutil.ReadFile(".env")
content, err := os.ReadFile(".env")
if err != nil {
return err
}

newContent := strings.Replace(string(content), "APP_KEY="+receiver.config.GetString("app.key"), "APP_KEY="+key, 1)

err = ioutil.WriteFile(".env", []byte(newContent), 0644)
err = os.WriteFile(".env", []byte(newContent), 0644)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions filesystem/application_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package filesystem

import (
"io/ioutil"
"io"
"mime"
"net/http"
"os"
Expand Down Expand Up @@ -357,7 +357,7 @@ func TestStorage(t *testing.T) {
if disk.disk != "local" && disk.disk != "custom" {
resp, err := http.Get(url)
assert.Nil(t, err)
content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
assert.Nil(t, resp.Body.Close())
assert.Nil(t, err)
assert.Equal(t, "Goravel", string(content), disk.disk)
Expand All @@ -375,7 +375,7 @@ func TestStorage(t *testing.T) {
if disk.disk != "local" && disk.disk != "custom" {
resp, err := http.Get(url)
assert.Nil(t, err)
content, err := ioutil.ReadAll(resp.Body)
content, err := io.ReadAll(resp.Body)
assert.Nil(t, resp.Body.Close())
assert.Nil(t, err)
assert.Equal(t, "Goravel", string(content), disk.disk)
Expand Down
23 changes: 15 additions & 8 deletions filesystem/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"os"
"path"
"path/filepath"
"strings"
"time"

Expand All @@ -34,7 +33,7 @@ func NewFile(file string) (*File, error) {
config: ConfigFacade,
disk: ConfigFacade.GetString("filesystems.default"),
path: file,
name: path.Base(file),
name: filepath.Base(file),
storage: StorageFacade,
}, nil
}
Expand All @@ -44,14 +43,22 @@ func NewFileFromRequest(fileHeader *multipart.FileHeader) (*File, error) {
if err != nil {
return nil, err
}
defer src.Close()

tempFileName := fmt.Sprintf("%s_*%s", ConfigFacade.GetString("app.name"), path.Ext(fileHeader.Filename))
tempFile, err := ioutil.TempFile(os.TempDir(), tempFileName)
defer func(src multipart.File) {
if err = src.Close(); err != nil {
panic(err)
}
}(src)

tempFileName := fmt.Sprintf("%s_*%s", ConfigFacade.GetString("app.name"), filepath.Ext(fileHeader.Filename))
tempFile, err := os.CreateTemp(os.TempDir(), tempFileName)
if err != nil {
return nil, err
}
defer tempFile.Close()
defer func(tempFile *os.File) {
if err = tempFile.Close(); err != nil {
panic(err)
}
}(tempFile)

_, err = io.Copy(tempFile, src)
if err != nil {
Expand Down
6 changes: 3 additions & 3 deletions filesystem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"mime/multipart"
"net/http"
"net/http/httptest"
"path"
"path/filepath"
"testing"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -82,9 +82,9 @@ func TestNewFileFromRequest(t *testing.T) {
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
assert.Nil(t, err)
file, err := NewFileFromRequest(f)
requestFile, err := NewFileFromRequest(f)
assert.Nil(t, err)
assert.Equal(t, ".txt", path.Ext(file.path))
assert.Equal(t, ".txt", filepath.Ext(requestFile.path))

mockConfig.AssertExpectations(t)
}
7 changes: 3 additions & 4 deletions filesystem/utils.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package filesystem

import (
"path"
"path/filepath"
"strings"

Expand All @@ -10,16 +9,16 @@ import (
)

func fullPathOfFile(filePath string, source filesystem.File, name string) (string, error) {
extension := path.Ext(name)
extension := filepath.Ext(name)
if extension == "" {
var err error
extension, err = file.Extension(source.File(), true)
if err != nil {
return "", err
}

return filepath.Join(filePath, strings.TrimSuffix(strings.TrimPrefix(path.Base(name), string(filepath.Separator)), string(filepath.Separator))+"."+extension), nil
return filepath.Join(filePath, strings.TrimSuffix(strings.TrimPrefix(filepath.Base(name), string(filepath.Separator)), string(filepath.Separator))+"."+extension), nil
} else {
return filepath.Join(filePath, strings.TrimPrefix(path.Base(name), string(filepath.Separator))), nil
return filepath.Join(filePath, strings.TrimPrefix(filepath.Base(name), string(filepath.Separator))), nil
}
}
8 changes: 3 additions & 5 deletions foundation/console/vendor_publish_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package console
import (
"errors"
"go/build"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"

Expand Down Expand Up @@ -164,7 +162,7 @@ func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, exi

var sourceFiles []string
if sourcePathStat.IsDir() {
fileInfos, err := ioutil.ReadDir(sourcePath)
fileInfos, err := os.ReadDir(sourcePath)
if err != nil {
return nil, err
}
Expand All @@ -177,7 +175,7 @@ func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, exi

for _, sourceFile := range sourceFiles {
targetFile := targetPath
if path.Ext(targetFile) == "" {
if filepath.Ext(targetFile) == "" {
targetFile = filepath.Join(targetFile, filepath.Base(sourceFile))
}

Expand All @@ -194,7 +192,7 @@ func (receiver *VendorPublishCommand) publish(sourcePath, targetPath string, exi
}

func (receiver *VendorPublishCommand) publishFile(sourceFile, targetFile string, existing, force bool) (bool, error) {
content, err := ioutil.ReadFile(sourceFile)
content, err := os.ReadFile(sourceFile)
if err != nil {
return false, err
}
Expand Down
Loading

0 comments on commit 87db2a7

Please sign in to comment.