delete unused images after documentation edit
parent
a08fdd918c
commit
eb3f565cec
|
@ -170,8 +170,6 @@ func GetEquipmentDocumentation(stockItemId string, documentationId string, c *fi
|
|||
func EditEquipmentDocumentation(c *fiber.Ctx, body structs.ApiEditEquipmentDocumentationRequest) error {
|
||||
var bodyNotes []map[string]string
|
||||
|
||||
log.Info().Msgf("EditEquipmentDocumentation")
|
||||
|
||||
err := json.Unmarshal(body.Notes, &bodyNotes)
|
||||
|
||||
if err != nil {
|
||||
|
@ -219,6 +217,30 @@ func EditEquipmentDocumentation(c *fiber.Ctx, body structs.ApiEditEquipmentDocum
|
|||
})
|
||||
}
|
||||
|
||||
// read all images on the folder and delete images that are not longer used in notes
|
||||
files, err := os.ReadDir(config.Cfg.FolderPaths.PublicStatic + "/equipmentdocumentation/" + body.DocumentationId)
|
||||
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to read directory, err: %s", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
|
||||
for _, file := range files {
|
||||
if file.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
if !isInList(file.Name(), notes) {
|
||||
err := os.Remove(config.Cfg.FolderPaths.PublicStatic + "/equipmentdocumentation/" + body.DocumentationId + "/" + file.Name())
|
||||
|
||||
if err != nil {
|
||||
log.Error().Msgf("Failed to remove file, err: %s", err)
|
||||
return c.SendStatus(fiber.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// marshal notes to json
|
||||
marshaledNotes, err := json.Marshal(notes)
|
||||
|
||||
if err != nil {
|
||||
|
@ -235,6 +257,16 @@ func EditEquipmentDocumentation(c *fiber.Ctx, body structs.ApiEditEquipmentDocum
|
|||
return c.JSON(fiber.Map{"message": "ok"})
|
||||
}
|
||||
|
||||
func isInList(fileName string, notes []Notes) bool {
|
||||
for _, note := range notes {
|
||||
if note.Image == fileName {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
// fetching the thumbnail from the invex server and sending it back to the client
|
||||
func GetEquipmentInvexThumbnail(c *fiber.Ctx, stockItemId string) error {
|
||||
// first request to /api/stock/:stockItemId/ to get the thumbnail url
|
||||
|
|
|
@ -215,11 +215,6 @@
|
|||
"summary": "Get equipment thumbnail",
|
||||
"operationId": "equipmentGetEquipmentThumbnail",
|
||||
"parameters": [
|
||||
{
|
||||
"description": "You can create a new api key in your user profile",
|
||||
"name": "X-Api-Key",
|
||||
"in": "header"
|
||||
},
|
||||
{
|
||||
"type": "string",
|
||||
"description": "Stock item id",
|
||||
|
|
|
@ -168,9 +168,6 @@ func GetEquipmentThumbnail(c *fiber.Ctx) error {
|
|||
// produces:
|
||||
// - application/json
|
||||
// parameters:
|
||||
// - name: X-Api-Key
|
||||
// in: header
|
||||
// description: You can create a new api key in your user profile
|
||||
// - name: stockItemId
|
||||
// in: path
|
||||
// description: Stock item id
|
||||
|
|
|
@ -42,7 +42,7 @@ func SetupRoutes(app *fiber.App) {
|
|||
e.Post("/documentation/create", requestAccessValidation, equipment.CreateEquipmentDocumentation)
|
||||
e.Get("/documentation/:stockItemId/:documentationId", requestAccessValidation, equipment.GetEquipmentDocumentation)
|
||||
e.Post("/documentation/edit", requestAccessValidation, equipment.EditEquipmentDocumentation)
|
||||
e.Get("/thumbnail/:stockItemId", requestAccessValidation, equipment.GetEquipmentThumbnail)
|
||||
e.Get("/thumbnail/:stockItemId", equipment.GetEquipmentThumbnail)
|
||||
|
||||
app.Static("/", config.Cfg.FolderPaths.PublicStatic)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue