2023-06-11 00:32:02 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2023-06-17 04:08:50 +00:00
|
|
|
"bytes"
|
2023-06-30 19:06:46 +00:00
|
|
|
"gosimplenpm/config"
|
2023-06-17 04:08:50 +00:00
|
|
|
"gosimplenpm/storage"
|
|
|
|
"io"
|
2023-06-11 00:32:02 +00:00
|
|
|
"net/http"
|
2023-06-17 04:08:50 +00:00
|
|
|
"net/url"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2023-06-30 19:06:46 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2023-06-11 00:32:02 +00:00
|
|
|
)
|
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
func PackageTarGet(lg *logrus.Logger, cfg config.Config) http.HandlerFunc {
|
|
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
// Sample output of npm view
|
|
|
|
// Public
|
|
|
|
// dist
|
|
|
|
// .tarball: https://registry.npmjs.org/react/-/react-18.2.0.tgz
|
|
|
|
// LocalHost
|
|
|
|
// dist
|
|
|
|
// .tarball: http://localhost:4000/@ookusanya/package1/-/package1-0.2.0.tgz
|
2023-06-17 04:08:50 +00:00
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
escapedName := mux.Vars(r)["name"]
|
|
|
|
packageName, _ := url.PathUnescape(escapedName)
|
|
|
|
lg.WithFields(logrus.Fields{
|
|
|
|
"function": "get-tar",
|
|
|
|
}).Debugf("Package name => %s\n", packageName)
|
|
|
|
escapedName = mux.Vars(r)["tar"]
|
|
|
|
tarFileName, _ := url.PathUnescape(escapedName)
|
|
|
|
lg.WithFields(logrus.Fields{
|
|
|
|
"function": "get-tar",
|
|
|
|
}).Debugf("Tarfile name => %s\n", tarFileName)
|
2023-06-17 04:08:50 +00:00
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
versionName := strings.Split(strings.Split(tarFileName, "-")[1], ".tgz")[0]
|
|
|
|
fileAsString, err := storage.GetTarFromStore(packageName, versionName, cfg.RepoDir, lg)
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
|
2023-06-30 19:06:46 +00:00
|
|
|
// Sending the tar as a base64 string
|
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
w.Header().Set("Content-Length", strconv.Itoa(len([]byte(fileAsString))))
|
|
|
|
io.Copy(w, bytes.NewReader([]byte(fileAsString)))
|
|
|
|
}
|
2023-06-11 00:32:02 +00:00
|
|
|
}
|