Przeglądaj źródła

Restored the old upload method

TC pushbot 5 4 lat temu
rodzic
commit
60a7dd0953
2 zmienionych plików z 131 dodań i 129 usunięć
  1. 98 129
      file_system.go
  2. 33 0
      mod/filesystem/upload/upload.go

+ 98 - 129
file_system.go

@@ -9,9 +9,11 @@ import (
 	"io/ioutil"
 	"log"
 	"math"
+	"mime/multipart"
 	"net/http"
 	"net/url"
 	"os"
+
 	"path/filepath"
 	"runtime"
 	"sort"
@@ -26,11 +28,11 @@ import (
 	fsp "imuslab.com/arozos/mod/filesystem/fspermission"
 	hidden "imuslab.com/arozos/mod/filesystem/hidden"
 	metadata "imuslab.com/arozos/mod/filesystem/metadata"
-	"imuslab.com/arozos/mod/filesystem/upload"
 	module "imuslab.com/arozos/mod/modules"
 	prout "imuslab.com/arozos/mod/prouter"
 	"imuslab.com/arozos/mod/share"
 	storage "imuslab.com/arozos/mod/storage"
+	user "imuslab.com/arozos/mod/user"
 )
 
 var (
@@ -510,8 +512,6 @@ func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	username := userinfo.Username
-
 	//Limit the max upload size to the user defined size
 	if max_upload_size != 0 {
 		r.Body = http.MaxBytesReader(w, r.Body, max_upload_size)
@@ -523,134 +523,74 @@ func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	uploadedFilepaths, err := upload.StreamUploadToDisk(userinfo, w, r)
+	err = r.ParseMultipartForm(int64(*upload_buf) << 20)
 	if err != nil {
-		sendErrorResponse(w, err.Error())
+		//Filesize too big
+		log.Println(err)
+		sendErrorResponse(w, "File too large")
 		return
 	}
 
-	//Set the ownership of file(s)
-	quotaFulled := false
-	for _, fileRealpath := range uploadedFilepaths {
-		//Check for storage quota
-		uploadFileSize := fs.GetFileSize(fileRealpath)
-		if !userinfo.StorageQuota.HaveSpace(uploadFileSize) {
-			//User storage quota fulled. Remove this file
-			quotaFulled = true
-
-			//Remove this file as this doesn't fit
-			os.Remove(fileRealpath)
-		} else {
-			userinfo.SetOwnerOfFile(fileRealpath)
-		}
+	file, handler, err := r.FormFile("file")
+	if err != nil {
+		log.Println("Error Retrieving File from upload by user: " + userinfo.Username)
+		sendErrorResponse(w, "Unable to parse file from upload")
+		return
 	}
 
-	if quotaFulled {
-		sendErrorResponse(w, "Storage Quota Full")
+	//Get upload target directory
+	uploadTarget, _ := mv(r, "path", true)
+	if uploadTarget == "" {
+		sendErrorResponse(w, "Upload target cannot be empty.")
 		return
 	}
 
-	sendOK(w)
-	return
-
-	/*
-		err = r.ParseMultipartForm(int64(*upload_buf) << 20)
-		if err != nil {
-			//Filesize too big
-			log.Println(err)
-			sendErrorResponse(w, "File too large")
-			return
-		}
-
-		file, handler, err := r.FormFile("file")
-		if err != nil {
-			log.Println("Error Retrieving File from upload by user: " + username)
-			sendErrorResponse(w, "Unable to parse file from upload")
-			return
-		}
-
-		//Get upload target directory
-		uploadTarget, _ := mv(r, "path", true)
-		if uploadTarget == "" {
-			sendErrorResponse(w, "Upload target cannot be empty.")
-			return
-		}
-
-		//Translate the upload target directory
-		realUploadPath, err := userinfo.VirtualPathToRealPath(uploadTarget)
-		if err != nil {
-			sendErrorResponse(w, "Upload target is invalid or permission denied.")
-			return
-		}
-
-		storeFilename := handler.Filename //Filename of the uploaded file
-		destFilepath := filepath.ToSlash(filepath.Clean(realUploadPath)) + "/" + storeFilename
-
-		if !fileExists(filepath.Dir(destFilepath)) {
-			os.MkdirAll(filepath.Dir(destFilepath), 0755)
-		}
-
-		//Check if the upload target is read only.
-		accmode := userinfo.GetPathAccessPermission(uploadTarget)
-		if accmode == "readonly" {
-			sendErrorResponse(w, "The upload target is Read Only.")
-			return
-		} else if accmode == "denied" {
-			sendErrorResponse(w, "Access Denied")
-			return
-		}
-
-		//Check for storage quota
-		uploadFileSize := handler.Size
-		if !userinfo.StorageQuota.HaveSpace(uploadFileSize) {
-			sendErrorResponse(w, "Storage Quota Full")
-			return
-		}
+	//Translate the upload target directory
+	realUploadPath, err := userinfo.VirtualPathToRealPath(uploadTarget)
+	if err != nil {
+		sendErrorResponse(w, "Upload target is invalid or permission denied.")
+		return
+	}
 
-		//Prepare the file to be created (uploaded)
-		destination, err := os.Create(destFilepath)
-		if err != nil {
-			sendErrorResponse(w, err.Error())
-			return
-		}
+	storeFilename := handler.Filename //Filename of the uploaded file
+	destFilepath := filepath.ToSlash(filepath.Clean(realUploadPath)) + "/" + storeFilename
 
-		defer destination.Close()
-		defer file.Close()
-
-		//Move the file to destination file location
-		if *enable_asyncFileUpload {
-			//Use Async upload method
-			go func(r *http.Request, file multipart.File, destination *os.File, userinfo *user.User) {
-				//Do the file copying using a buffered reader
-				buf := make([]byte, *file_opr_buff)
-				for {
-					n, err := file.Read(buf)
-					if err != nil && err != io.EOF {
-						log.Println(err.Error())
-						return
-					}
-					if n == 0 {
-						break
-					}
+	if !fileExists(filepath.Dir(destFilepath)) {
+		os.MkdirAll(filepath.Dir(destFilepath), 0755)
+	}
 
-					if _, err := destination.Write(buf[:n]); err != nil {
-						log.Println(err.Error())
-						return
-					}
-				}
+	//Check if the upload target is read only.
+	accmode := userinfo.GetPathAccessPermission(uploadTarget)
+	if accmode == "readonly" {
+		sendErrorResponse(w, "The upload target is Read Only.")
+		return
+	} else if accmode == "denied" {
+		sendErrorResponse(w, "Access Denied")
+		return
+	}
 
-				//Clear up buffered files
-				r.MultipartForm.RemoveAll()
+	//Check for storage quota
+	uploadFileSize := handler.Size
+	if !userinfo.StorageQuota.HaveSpace(uploadFileSize) {
+		sendErrorResponse(w, "Storage Quota Full")
+		return
+	}
 
-				//Set the ownership of file
-				userinfo.SetOwnerOfFile(destFilepath)
+	//Prepare the file to be created (uploaded)
+	destination, err := os.Create(destFilepath)
+	if err != nil {
+		sendErrorResponse(w, err.Error())
+		return
+	}
 
-				//Perform a GC afterward
-				runtime.GC()
+	defer destination.Close()
+	defer file.Close()
 
-			}(r, file, destination, userinfo)
-		} else {
-			//Use blocking upload and move method
+	//Move the file to destination file location
+	if *enable_asyncFileUpload {
+		//Use Async upload method
+		go func(r *http.Request, file multipart.File, destination *os.File, userinfo *user.User) {
+			//Do the file copying using a buffered reader
 			buf := make([]byte, *file_opr_buff)
 			for {
 				n, err := file.Read(buf)
@@ -673,27 +613,56 @@ func system_fs_handleUpload(w http.ResponseWriter, r *http.Request) {
 
 			//Set the ownership of file
 			userinfo.SetOwnerOfFile(destFilepath)
+
+			//Perform a GC afterward
+			runtime.GC()
+
+		}(r, file, destination, userinfo)
+	} else {
+		//Use blocking upload and move method
+		buf := make([]byte, *file_opr_buff)
+		for {
+			n, err := file.Read(buf)
+			if err != nil && err != io.EOF {
+				log.Println(err.Error())
+				return
+			}
+			if n == 0 {
+				break
+			}
+
+			if _, err := destination.Write(buf[:n]); err != nil {
+				log.Println(err.Error())
+				return
+			}
 		}
 
-		//Finish up the upload
+		//Clear up buffered files
+		r.MultipartForm.RemoveAll()
 
-		//fmt.Printf("Uploaded File: %+v\n", handler.Filename)
-		//fmt.Printf("File Size: %+v\n", handler.Size)
-		//fmt.Printf("MIME Header: %+v\n", handler.Header)
-		//fmt.Println("Upload target: " + realUploadPath)
+		//Set the ownership of file
+		userinfo.SetOwnerOfFile(destFilepath)
+	}
 
-		//Fnish upload. Fix the tmp filename
-		log.Println(username + " uploaded a file: " + handler.Filename)
+	//Finish up the upload
 
-		//Do upload finishing stuff
-		//Perform a GC
-		runtime.GC()
+	//fmt.Printf("Uploaded File: %+v\n", handler.Filename)
+	//fmt.Printf("File Size: %+v\n", handler.Size)
+	//fmt.Printf("MIME Header: %+v\n", handler.Header)
+	//fmt.Println("Upload target: " + realUploadPath)
 
-		//Completed
-		sendOK(w)
+	//Fnish upload. Fix the tmp filename
+	log.Println(userinfo.Username + " uploaded a file: " + handler.Filename)
+
+	//Do upload finishing stuff
+	//Perform a GC
+	runtime.GC()
+
+	//Completed
+	sendOK(w)
+
+	return
 
-		return
-	*/
 }
 
 //Validate if the copy and target process will involve file overwriting problem.

+ 33 - 0
mod/filesystem/upload/upload.go

@@ -16,6 +16,39 @@ type chunk struct {
 	DestFilename string
 }
 
+/*
+	//Usage:
+	uploadedFilepaths, err := upload.StreamUploadToDisk(userinfo, w, r)
+	if err != nil {
+		sendErrorResponse(w, err.Error())
+		return
+	}
+
+	//Set the ownership of file(s)
+	quotaFulled := false
+	for _, fileRealpath := range uploadedFilepaths {
+		//Check for storage quota
+		uploadFileSize := fs.GetFileSize(fileRealpath)
+		if !userinfo.StorageQuota.HaveSpace(uploadFileSize) {
+			//User storage quota fulled. Remove this file
+			quotaFulled = true
+
+			//Remove this file as this doesn't fit
+			os.Remove(fileRealpath)
+		} else {
+			userinfo.SetOwnerOfFile(fileRealpath)
+		}
+	}
+
+	if quotaFulled {
+		sendErrorResponse(w, "Storage Quota Full")
+		return
+	}
+
+	sendOK(w)
+	return
+*/
+
 func StreamUploadToDisk(userinfo *user.User, w http.ResponseWriter, r *http.Request) ([]string, error) {
 	//Check if this userinfo is valid
 	if userinfo == nil {