From 17d3c3a525ce9c7bedbcdbf4a9aa1cd7d257b8f0 Mon Sep 17 00:00:00 2001 From: NOCanoa <158103554+NOCanoa@users.noreply.github.com> Date: Sun, 3 Nov 2024 23:38:09 +0000 Subject: [PATCH] upload --- .github/workflows/deploy.yml | 45 ++++++++++++++++ upload/go.mod | 3 ++ upload/main.go | 101 +++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+) create mode 100644 .github/workflows/deploy.yml create mode 100644 upload/go.mod create mode 100644 upload/main.go diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..8c58810 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,45 @@ +name: Upload to bunny + +on: + workflow_dispatch: + push: + branches: + - main + +jobs: + upload: + runs-on: ubuntu-latest + strategy: + matrix: + go-version: [1.22.3] + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + + - uses: actions/setup-node@v4 + with: + node-version: 22 + + - name: get pnpm + run: npm i -g pnpm + + - name: dependencies + run: pnpm install + + - name: buld + run: pnpm run build + + - name: Set up Go + uses: actions/setup-go@v5 + with: + go-version: ${{ matrix.go-version }} + + - name: List Directory Contents + run: ls + - name: Change Directory and Run Go File + run: | + cd upload + go run main.go ${{ secrets.STORAGE_PASSWORD }} ${{ secrets.STORAGE_NAME }} + diff --git a/upload/go.mod b/upload/go.mod new file mode 100644 index 0000000..210f8b6 --- /dev/null +++ b/upload/go.mod @@ -0,0 +1,3 @@ +module zen-site-astro + +go 1.22.3 diff --git a/upload/main.go b/upload/main.go new file mode 100644 index 0000000..d2b27f8 --- /dev/null +++ b/upload/main.go @@ -0,0 +1,101 @@ +package main + +import ( + "bytes" + "fmt" + "log" + "net/http" + "os" + "path/filepath" + "sync" + "time" +) + +var semaphore = make(chan bool, 45) + +func uploadFile(storageZoneName, region, apiKey, filePath string) error { + semaphore <- true + defer func() { <-semaphore }() + + fileBytes, err := os.ReadFile(filePath) + if err != nil { + return err + } + relPath, err := filepath.Rel("../dist", filePath) + if err != nil { + return err + } + req, err := http.NewRequest("PUT", fmt.Sprintf("https://%s.bunnycdn.com/%s/%s", region, storageZoneName, relPath), bytes.NewReader(fileBytes)) + if err != nil { + return err + } + req.Header.Set("AccessKey", apiKey) + req.Header.Set("Content-Type", "application/octet-stream") + req.Header.Set("Accept", "application/json") + resp, err := http.DefaultClient.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { + return fmt.Errorf("upload failed with status code %d", resp.StatusCode) + } + log.Printf("Uploaded %s to BunnyStorage\n", relPath) + return nil +} + +func uploadFilesToBunnyCDN(storageZoneName, region, apiKey string) error { + distDir := "../dist" + var wg sync.WaitGroup + errChan := make(chan error) + + filepath.Walk(distDir, func(path string, info os.FileInfo, err error) error { + if err != nil { + return err + } + if info.Name() == ".git" { + return filepath.SkipDir + } + if !info.IsDir() { + wg.Add(1) + go func(filePath string) { + defer wg.Done() + if err := uploadFile(storageZoneName, region, apiKey, filePath); err != nil { + errChan <- err + } + }(path) + } + return nil + }) + + go func() { + wg.Wait() + close(errChan) + }() + + for err := range errChan { + return err + } + + return nil +} + +func main() { + start := time.Now() + storageZoneName := "no" + region := "storage" + apiKey := "hehe" + + if len(os.Args) >= 3 { + apiKey = os.Args[1] + } + if len(os.Args) >= 3 { + storageZoneName = os.Args[2] + } + + if err := uploadFilesToBunnyCDN(storageZoneName, region, apiKey); err != nil { + log.Fatal(err) + } + elapsed := time.Since(start) + log.Printf("Successfully uploaded files to BunnyCDN in %v\n", elapsed) +}