-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: support import theme from git repo
- Loading branch information
Showing
9 changed files
with
243 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package theme | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/go-git/go-git/v5" | ||
"go.uber.org/fx" | ||
|
||
"github.com/go-sonic/sonic/model/dto" | ||
"github.com/go-sonic/sonic/util/xerr" | ||
) | ||
|
||
type gitThemeFetcherImpl struct { | ||
fx.Out | ||
PropertyScanner PropertyScanner | ||
} | ||
|
||
func (g gitThemeFetcherImpl) FetchTheme(ctx context.Context, file interface{}) (*dto.ThemeProperty, error) { | ||
gitURL := file.(string) | ||
splits := strings.Split(gitURL, "/") | ||
lastSplit := splits[len(splits)-1] | ||
tempDir := os.TempDir() | ||
|
||
themeDirName := lastSplit | ||
_, err := git.PlainClone(filepath.Join(tempDir, themeDirName), false, &git.CloneOptions{ | ||
URL: gitURL, | ||
}) | ||
if err != nil { | ||
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg(err.Error()) | ||
} | ||
themeProperty, err := g.PropertyScanner.ReadThemeProperty(ctx, filepath.Join(tempDir, themeDirName)) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return themeProperty, nil | ||
} | ||
|
||
func NewGitThemeFetcher(propertyScanner PropertyScanner) ThemeFetcher { | ||
return &gitThemeFetcherImpl{ | ||
PropertyScanner: propertyScanner, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,16 @@ | ||
package theme | ||
|
||
import "github.com/go-sonic/sonic/injection" | ||
import ( | ||
"go.uber.org/fx" | ||
|
||
"github.com/go-sonic/sonic/injection" | ||
) | ||
|
||
func init() { | ||
injection.Provide( | ||
NewFileScanner, | ||
NewPropertyScanner, | ||
NewMultipartZipThemeFetcher, | ||
fx.Annotated{Target: NewMultipartZipThemeFetcher, Name: "multipartZipThemeFetcher"}, | ||
fx.Annotated{Target: NewGitThemeFetcher, Name: "gitRepoThemeFetcher"}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package theme | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"mime/multipart" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"go.uber.org/fx" | ||
|
||
"github.com/go-sonic/sonic/model/dto" | ||
"github.com/go-sonic/sonic/util" | ||
"github.com/go-sonic/sonic/util/xerr" | ||
) | ||
|
||
type multipartZipThemeFetcherImpl struct { | ||
fx.Out | ||
PropertyScanner PropertyScanner | ||
} | ||
|
||
func NewMultipartZipThemeFetcher(propertyScanner PropertyScanner) ThemeFetcher { | ||
return &multipartZipThemeFetcherImpl{ | ||
PropertyScanner: propertyScanner, | ||
} | ||
} | ||
|
||
func (m *multipartZipThemeFetcherImpl) FetchTheme(ctx context.Context, file interface{}) (*dto.ThemeProperty, error) { | ||
themeFileHeader, ok := file.(*multipart.FileHeader) | ||
if !ok { | ||
return nil, xerr.WithStatus(nil, xerr.StatusBadRequest).WithMsg("not support") | ||
} | ||
|
||
tempDir := os.TempDir() | ||
srcThemeFile, err := themeFileHeader.Open() | ||
if err != nil { | ||
return nil, xerr.WithStatus(err, xerr.StatusBadRequest).WithMsg("upload theme file error") | ||
} | ||
defer srcThemeFile.Close() | ||
|
||
fileName := themeFileHeader.Filename | ||
if !strings.HasSuffix(fileName, ".zip") { | ||
return nil, xerr.WithStatus(nil, xerr.StatusBadRequest).WithMsg("not zip file") | ||
} | ||
|
||
diskFilePath := filepath.Join(tempDir, fileName) | ||
if util.FileIsExisted(diskFilePath) { | ||
err = os.Remove(diskFilePath) | ||
if err != nil { | ||
return nil, xerr.WithStatus(err, xerr.StatusInternalServerError) | ||
} | ||
} | ||
|
||
diskFile, err := os.OpenFile(diskFilePath, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0o444) | ||
if err != nil && !errors.Is(err, os.ErrExist) { | ||
return nil, xerr.WithStatus(err, xerr.StatusInternalServerError).WithMsg("create file error") | ||
} | ||
|
||
defer diskFile.Close() | ||
|
||
_, err = io.Copy(diskFile, srcThemeFile) | ||
if err != nil { | ||
return nil, xerr.WithStatus(err, xerr.StatusInternalServerError).WithMsg("save file error") | ||
} | ||
_, err = util.Unzip(filepath.Join(tempDir, fileName), filepath.Join(tempDir, strings.TrimSuffix(fileName, ".zip"))) | ||
if err != nil { | ||
return nil, xerr.WithStatus(err, xerr.StatusInternalServerError).WithMsg("unzip file error") | ||
} | ||
dest := filepath.Join(tempDir, strings.TrimSuffix(fileName, ".zip")) | ||
themeProperty, err := m.PropertyScanner.ReadThemeProperty(ctx, dest) | ||
if err == nil && themeProperty != nil { | ||
return themeProperty, nil | ||
} | ||
dirEntrys, err := os.ReadDir(dest) | ||
for _, dirEntry := range dirEntrys { | ||
if !dirEntry.IsDir() { | ||
continue | ||
} | ||
themeProperty, err = m.PropertyScanner.ReadThemeProperty(ctx, filepath.Join(dest, dirEntry.Name())) | ||
if err == nil && themeProperty != nil { | ||
return themeProperty, nil | ||
} | ||
} | ||
return nil, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters