Skip to content
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

New File Sever #191

Merged
merged 2 commits into from
Oct 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions backend/configs/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
)

var DB *gorm.DB
var FileServer = "http://ec2-13-57-42-212.us-west-1.compute.amazonaws.com/files/"

func SetupDB() {
envs := EnvPG()
Expand Down
52 changes: 50 additions & 2 deletions backend/controllers/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"backend/utils"
"fmt"
"github.com/gin-gonic/gin"
"io"
"math/rand"
"net/http"
"os"
Expand Down Expand Up @@ -140,7 +141,54 @@ func deleteFilesWithPrefix(directory, prefix string) error {
func GetFile() gin.HandlerFunc {
return func(c *gin.Context) {
filename := c.Param("filename")
filePath := "./uploads/" + filename
c.File(filePath) // Esto sirve el archivo al cliente
fileURL := configs.FileServer + filename

// Realizar una solicitud GET al servidor de archivos externo
resp, err := http.Get(fileURL)
if err != nil {
c.JSON(http.StatusNotFound, responses.StandardResponse{
Status: http.StatusNotFound,
Message: "Error al obtener el archivo: " + err.Error(),
Data: nil,
})
return
}

defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
c.JSON(http.StatusInternalServerError, responses.StandardResponse{
Status: http.StatusInternalServerError,
Message: "Error al cerrar el cuerpo de la respuesta del servidor de archivos externo: " + err.Error(),
Data: nil,
})
return
}
}(resp.Body)

if resp.StatusCode != http.StatusOK {
c.JSON(http.StatusNotFound, responses.StandardResponse{
Status: http.StatusNotFound,
Message: "Archivo no encontrado en el servidor de archivos externo",
Data: nil,
})
return
}

// Configurar las cabeceras de la respuesta para el cliente
c.Header("Content-Type", resp.Header.Get("Content-Type"))
c.Header("Content-Disposition", "inline; filename="+filename)
c.Header("Content-Length", resp.Header.Get("Content-Length"))

// Copiar el cuerpo de la respuesta del servidor de archivos al cuerpo de la respuesta de Gin
_, err = io.Copy(c.Writer, resp.Body)
if err != nil {
c.JSON(http.StatusInternalServerError, responses.StandardResponse{
Status: http.StatusInternalServerError,
Message: "Error al copiar el cuerpo de la respuesta del servidor de archivos externo: " + err.Error(),
Data: nil,
})
return
}
}
}
1 change: 0 additions & 1 deletion backend/routes/routes_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ func Routes(router *gin.Engine) {
public.GET("/careers", controllers.GetCareers)

// Empresas
// Ale: Use "company" porque el mamark quería que fuera en inglés :)
companies := router.Group("api/companies")
companies.Use(middlewares.JwtAuthentication())

Expand Down
Loading