Improve file writing logic

This commit is contained in:
raul 2024-05-21 08:52:26 +02:00
parent e9e9c670be
commit 12d3498055
2 changed files with 12 additions and 11 deletions

View File

@ -26,16 +26,7 @@ var serveCmd = &cobra.Command{
func init() { func init() {
rootCmd.AddCommand(serveCmd) rootCmd.AddCommand(serveCmd)
serveCmd.PersistentFlags().StringP("port", "p", "1302", "Port for server to listen on")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
serveCmd.PersistentFlags().String("port", "1302", "Port for server to listen on")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// serveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
} }
func setServerParameters(cmd *cobra.Command) error { func setServerParameters(cmd *cobra.Command) error {

View File

@ -6,6 +6,7 @@ import (
"io" "io"
"io/fs" "io/fs"
"log" "log"
"mime/multipart"
"net/http" "net/http"
"os" "os"
) )
@ -38,11 +39,20 @@ func uploadFile(w http.ResponseWriter, r *http.Request) {
if err != nil { if err != nil {
log.Printf("Error happened opening file: %v\n", err) log.Printf("Error happened opening file: %v\n", err)
} }
isDone := make(chan bool)
_, err = io.Copy(f, file) go copyTo(isDone, f, file)
<-isDone
log.Printf("Successfully copied file!\n")
}
func copyTo(isDone chan bool, f *os.File, file multipart.File) {
_, err := io.Copy(f, file)
if err != nil { if err != nil {
log.Printf("Error happened writing file: %v\n", err) log.Printf("Error happened writing file: %v\n", err)
} }
isDone <- true
} }
func server() { func server() {