29 lines
621 B
Go
29 lines
621 B
Go
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)
|
|
}
|
|
}
|