forked from jason/cpolis
		
	Create WriteFile() so backend handles files
This commit is contained in:
		@@ -3,6 +3,7 @@ package backend
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"os"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
@@ -19,10 +20,30 @@ func CopyFile(src, dst string) error {
 | 
			
		||||
	}
 | 
			
		||||
	defer dstFile.Close()
 | 
			
		||||
 | 
			
		||||
	_, err = io.Copy(dstFile, srcFile)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
	if _, err = io.Copy(dstFile, srcFile); err != nil {
 | 
			
		||||
		return fmt.Errorf("error copying file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return dstFile.Sync()
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func WriteFile(path string, src io.Reader) error {
 | 
			
		||||
	file, err := os.Create(path)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return fmt.Errorf("error creating file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
	defer file.Close()
 | 
			
		||||
 | 
			
		||||
	if _, err = io.Copy(file, src); err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return fmt.Errorf("error copying file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	if err = file.Sync(); err != nil {
 | 
			
		||||
		log.Println(err)
 | 
			
		||||
		return fmt.Errorf("error syncing file: %v", err)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return nil
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,10 +2,8 @@ package frontend
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"io"
 | 
			
		||||
	"log"
 | 
			
		||||
	"net/http"
 | 
			
		||||
	"os"
 | 
			
		||||
	"path/filepath"
 | 
			
		||||
 | 
			
		||||
	"github.com/google/uuid"
 | 
			
		||||
@@ -60,15 +58,7 @@ func UploadPDF(c *b.Config, s *b.CookieStore) http.HandlerFunc {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		pdf, err := os.Create(absFilepath)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		defer pdf.Close()
 | 
			
		||||
 | 
			
		||||
		if _, err = io.Copy(pdf, file); err != nil {
 | 
			
		||||
		if err = b.WriteFile(absFilepath, file); err != nil {
 | 
			
		||||
			log.Println(err)
 | 
			
		||||
			http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
			return
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user