-
Notifications
You must be signed in to change notification settings - Fork 103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Added option to save files in archive #564
Draft
MrCyjaneK
wants to merge
2
commits into
bleenco:master
Choose a base branch
from
MrCyjaneK:actualcd
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package build | ||
|
||
import ( | ||
"net/http" | ||
"os" | ||
"path" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/bleenco/abstruse/server/api/render" | ||
"github.com/bleenco/abstruse/server/config" | ||
"github.com/bleenco/abstruse/server/core" | ||
"github.com/go-chi/chi" | ||
) | ||
|
||
func HandleArchive(jobs core.JobStore, config *config.Config) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
id, err := strconv.Atoi(chi.URLParam(r, "id")) | ||
if err != nil { | ||
render.BadRequestError(w, err.Error()) | ||
return | ||
} | ||
|
||
file := path.Join(config.DataDir, "archive", strconv.Itoa(id), r.URL.Path[len(strconv.Itoa(id))+9:]) | ||
f, err := os.Open(file) | ||
if err != nil { | ||
render.NotFoundError(w, err.Error()) | ||
return | ||
} | ||
defer f.Close() | ||
http.ServeContent(w, r, "", time.Now(), f) | ||
} | ||
} |
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,59 @@ | ||
package worker | ||
|
||
import ( | ||
"io" | ||
"log" | ||
"net/http" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/bleenco/abstruse/server/api/render" | ||
"github.com/bleenco/abstruse/server/config" | ||
"github.com/mholt/archiver/v3" | ||
) | ||
|
||
// HandleUploadArchive returns http.handlerFunc that writes JSON encoded | ||
// result about uploading cache to the http response body. | ||
func HandleUploadArchive(config *config.Config) http.HandlerFunc { | ||
return func(w http.ResponseWriter, r *http.Request) { | ||
r.ParseMultipartForm(1000 << 20) | ||
|
||
src, handler, err := r.FormFile("file") | ||
if err != nil { | ||
render.InternalServerError(w, err.Error()) | ||
return | ||
} | ||
defer src.Close() | ||
os.MkdirAll(filepath.Join(config.DataDir, "archive"), 0750) | ||
filePath := filepath.Join(config.DataDir, "archive", handler.Filename) | ||
dst, err := os.Create(filePath) | ||
if err != nil { | ||
render.InternalServerError(w, err.Error()) | ||
return | ||
} | ||
defer dst.Close() | ||
|
||
if _, err := io.Copy(dst, src); err != nil { | ||
render.InternalServerError(w, err.Error()) | ||
return | ||
} | ||
|
||
if err = treatArchive(filePath, config.DataDir); err != nil { | ||
render.InternalServerError(w, err.Error()) | ||
return | ||
} | ||
render.JSON(w, http.StatusOK, render.BoolResponse{Status: true}) | ||
} | ||
} | ||
|
||
func treatArchive(filePath, datadir string) error { | ||
idSplit := strings.Split(strings.ReplaceAll(filePath, "/", "."), ".") | ||
// The job id | ||
targetDir := path.Join(datadir, "archive", idSplit[len(idSplit)-2]) | ||
os.MkdirAll(targetDir, 0750) | ||
log.Println(filePath, "/", targetDir) | ||
defer os.RemoveAll(filePath) | ||
return archiver.Unarchive(filePath, targetDir) | ||
} |
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
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,95 @@ | ||
package archive | ||
|
||
import ( | ||
"archive/tar" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
api "github.com/bleenco/abstruse/pb" | ||
"github.com/bleenco/abstruse/pkg/fs" | ||
gzip "github.com/klauspost/pgzip" | ||
) | ||
|
||
func SaveArchive(job *api.Job, dir string) (string, error) { | ||
fileName := fmt.Sprintf("%d.tgz", job.GetId()) | ||
out := filepath.Join(dir, fileName) | ||
|
||
if fs.Exists(out) { | ||
if err := os.RemoveAll(out); err != nil { | ||
return out, err | ||
} | ||
} | ||
|
||
return out, createArchive(job.GetArchive(), out) | ||
} | ||
|
||
func createArchive(folders []string, outPath string) error { | ||
out, err := os.Create(outPath) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
gw, err := gzip.NewWriterLevel(out, gzip.BestSpeed) | ||
if err != nil { | ||
return err | ||
} | ||
defer gw.Close() | ||
|
||
tw := tar.NewWriter(gw) | ||
defer tw.Close() | ||
|
||
for _, folder := range folders { | ||
folder = filepath.Join(filepath.Dir(outPath), folder) | ||
if err := filepath.Walk(folder, func(path string, info os.FileInfo, err error) error { | ||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
link := "" | ||
if info.Mode()&os.ModeSymlink != 0 { | ||
link, err = os.Readlink(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
header, err := tar.FileInfoHeader(info, link) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
header.Name = strings.TrimPrefix(path, fmt.Sprintf("%s/", filepath.Dir(outPath))) | ||
|
||
if err := tw.WriteHeader(header); err != nil { | ||
return err | ||
} | ||
|
||
switch header.Typeflag { | ||
case tar.TypeLink, tar.TypeSymlink, tar.TypeChar, tar.TypeBlock, tar.TypeDir, tar.TypeFifo: | ||
default: | ||
file, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if _, err := io.Copy(tw, file); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
}); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need extra help with this line - it doesn't work in a way in which I'd expect it to work... I get a 404 instead of the response from function, when requesting
/archive/{id}/file.txt