feat: archiver 3
This commit is contained in:
parent
dae992d4a9
commit
36f145790d
29
lib/file.go
29
lib/file.go
@ -1,7 +1,6 @@
|
|||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"crypto/sha1"
|
"crypto/sha1"
|
||||||
"crypto/sha256"
|
"crypto/sha256"
|
||||||
@ -19,7 +18,6 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/gohugoio/hugo/parser"
|
|
||||||
"github.com/maruel/natural"
|
"github.com/maruel/natural"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -178,33 +176,6 @@ func (i *File) GetListing(u *User, r *http.Request) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetEditor gets the editor based on a Info struct
|
|
||||||
func (i *File) GetEditor() error {
|
|
||||||
i.Language = editorLanguage(i.Extension)
|
|
||||||
// If the editor will hold only content, leave now.
|
|
||||||
if editorMode(i.Language) == "content" {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// If the file doesn't have any kind of metadata, leave now.
|
|
||||||
if !hasRune(i.Content) {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
buffer := bytes.NewBuffer([]byte(i.Content))
|
|
||||||
page, err := parser.ReadFrom(buffer)
|
|
||||||
|
|
||||||
// If there is an error, just ignore it and return nil.
|
|
||||||
// This way, the file can be served for editing.
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
i.Content = strings.TrimSpace(string(page.Content()))
|
|
||||||
i.Metadata = strings.TrimSpace(string(page.FrontMatter()))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetFileType obtains the mimetype and converts it to a simple
|
// GetFileType obtains the mimetype and converts it to a simple
|
||||||
// type nomenclature.
|
// type nomenclature.
|
||||||
func (i *File) GetFileType(checkContent bool) error {
|
func (i *File) GetFileType(checkContent bool) error {
|
||||||
|
|||||||
@ -45,21 +45,21 @@ func downloadHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
extension string
|
extension string
|
||||||
ar archiver.Archiver
|
ar archiver.Writer
|
||||||
)
|
)
|
||||||
|
|
||||||
switch query {
|
switch query {
|
||||||
// If the format is true, just set it to "zip".
|
// If the format is true, just set it to "zip".
|
||||||
case "zip", "true", "":
|
case "zip", "true", "":
|
||||||
extension, ar = ".zip", archiver.Zip
|
extension, ar = ".zip", &archiver.Zip{}
|
||||||
case "tar":
|
case "tar":
|
||||||
extension, ar = ".tar", archiver.Tar
|
extension, ar = ".tar", &archiver.Tar{}
|
||||||
case "targz":
|
case "targz":
|
||||||
extension, ar = ".tar.gz", archiver.TarGz
|
extension, ar = ".tar.gz", &archiver.TarGz{}
|
||||||
case "tarbz2":
|
case "tarbz2":
|
||||||
extension, ar = ".tar.bz2", archiver.TarBz2
|
extension, ar = ".tar.bz2", &archiver.TarBz2{}
|
||||||
case "tarxz":
|
case "tarxz":
|
||||||
extension, ar = ".tar.xz", archiver.TarXZ
|
extension, ar = ".tar.xz", &archiver.TarXz{}
|
||||||
default:
|
default:
|
||||||
return http.StatusNotImplemented, nil
|
return http.StatusNotImplemented, nil
|
||||||
}
|
}
|
||||||
@ -72,7 +72,34 @@ func downloadHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (int
|
|||||||
name += extension
|
name += extension
|
||||||
|
|
||||||
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
|
w.Header().Set("Content-Disposition", "attachment; filename*=utf-8''"+url.PathEscape(name))
|
||||||
err := ar.Write(w, files)
|
|
||||||
|
err := ar.Create(w)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer ar.Close()
|
||||||
|
|
||||||
|
for _, f := range files {
|
||||||
|
file, err := os.Open(f)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := file.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... open file and get the name for it within the archive ...
|
||||||
|
err = ar.Write(archiver.File{
|
||||||
|
FileInfo: info,
|
||||||
|
ReadCloser: file,
|
||||||
|
})
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|||||||
@ -95,12 +95,6 @@ func resourceGetHandler(c *fb.Context, w http.ResponseWriter, r *http.Request) (
|
|||||||
}
|
}
|
||||||
|
|
||||||
f.Kind = "editor"
|
f.Kind = "editor"
|
||||||
|
|
||||||
// Tries to get the editor data.
|
|
||||||
if err = f.GetEditor(); err != nil {
|
|
||||||
return http.StatusInternalServerError, err
|
|
||||||
}
|
|
||||||
|
|
||||||
return renderJSON(w, f)
|
return renderJSON(w, f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user