add timestamp to filename support with force name function
This commit is contained in:
parent
4b8954d540
commit
40ea8d95df
2 changed files with 102 additions and 89 deletions
21
go/main.go
21
go/main.go
|
@ -91,7 +91,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Zurücksetzen des Dateizeigers, um die Datei vollständig zu kopieren
|
||||
_, err = file.Seek(0, io.SeekStart)
|
||||
if err != nil {
|
||||
http.Error(w, "Fehler beim Zurücksetzen des Dateizeigers", http.StatusInternalServerError)
|
||||
|
@ -99,8 +98,20 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
return
|
||||
}
|
||||
|
||||
// Hier können Sie den Dateinamen manipulieren oder einen anderen Speicherort wählen
|
||||
uploadPath := "./uploads/" + handler.Filename
|
||||
// Ermitteln, ob der ursprüngliche Dateiname erzwungen werden soll
|
||||
//forceName := r.Header.Get("Force-Name")
|
||||
forceName := r.FormValue("force_name")
|
||||
var filename string
|
||||
if forceName == "true" {
|
||||
filename = handler.Filename
|
||||
} else {
|
||||
// Zeitstempel zum Dateinamen hinzufügen
|
||||
timestamp := time.Now().Format("20060102-150405")
|
||||
filename = fmt.Sprintf("%s-%s", timestamp, handler.Filename)
|
||||
}
|
||||
|
||||
// Datei speichern
|
||||
uploadPath := "./uploads/" + filename
|
||||
f, err := os.Create(uploadPath)
|
||||
if err != nil {
|
||||
http.Error(w, "Fehler beim Erstellen der Datei", http.StatusInternalServerError)
|
||||
|
@ -118,7 +129,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
|
||||
lastUploadTime = time.Now() // Setzen Sie die Zeit des letzten Uploads
|
||||
|
||||
// Nach erfolgreichem Upload:
|
||||
tmpl, err := template.ParseFiles("templates/uploadSuccess.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Fehler beim Laden des Templates", http.StatusInternalServerError)
|
||||
|
@ -131,7 +141,7 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
Filename string
|
||||
}{
|
||||
Message: "Bild erfolgreich hochgeladen.",
|
||||
Filename: handler.Filename,
|
||||
Filename: filename, // Geändert, um den möglicherweise modifizierten Dateinamen anzuzeigen
|
||||
}
|
||||
|
||||
err = tmpl.Execute(w, data)
|
||||
|
@ -142,7 +152,6 @@ func uploadHandler(w http.ResponseWriter, r *http.Request) {
|
|||
}
|
||||
|
||||
} else {
|
||||
|
||||
tmpl, err := template.ParseFiles("templates/uploadForm.html")
|
||||
if err != nil {
|
||||
http.Error(w, "Fehler beim Laden des Templates", http.StatusInternalServerError)
|
||||
|
|
|
@ -5,7 +5,11 @@
|
|||
</head>
|
||||
<body>
|
||||
<form action="/upload" method="post" enctype="multipart/form-data">
|
||||
<input type="file" name="image">
|
||||
<input type="file" name="image" required>
|
||||
<br>
|
||||
<input type="checkbox" name="force_name" value="true">
|
||||
<label for="force_name">Originalnamen beibehalten (Force Name)</label>
|
||||
<br>
|
||||
<input type="submit" value="Hochladen">
|
||||
</form>
|
||||
</body>
|
||||
|
|
Loading…
Reference in a new issue