first commit
This commit is contained in:
28
internal/pkg/utils/file.go
Normal file
28
internal/pkg/utils/file.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DeleteLocalFile menghapus file lokal jika file tersebut berada di folder ./public
|
||||
func DeleteLocalFile(relPath string) {
|
||||
if relPath == "" || strings.HasPrefix(relPath, "http://") || strings.HasPrefix(relPath, "https://") {
|
||||
return
|
||||
}
|
||||
|
||||
cleaned := filepath.Clean(relPath)
|
||||
var localPath string
|
||||
if strings.HasPrefix(cleaned, "/images/") {
|
||||
localPath = "./public" + cleaned
|
||||
} else if strings.HasPrefix(cleaned, "/public/") {
|
||||
localPath = "." + cleaned
|
||||
} else {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := os.Stat(localPath); err == nil {
|
||||
_ = os.Remove(localPath)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user