diff --git a/Admin/HandlerConfiguration.go b/Admin/HandlerConfiguration.go new file mode 100644 index 0000000..f92a040 --- /dev/null +++ b/Admin/HandlerConfiguration.go @@ -0,0 +1,64 @@ +package Admin + +import ( + "github.com/SommerEngineering/Ocean/ConfigurationDB" + "github.com/SommerEngineering/Ocean/Log" + LM "github.com/SommerEngineering/Ocean/Log/Meta" + "github.com/SommerEngineering/Ocean/MimeTypes" + "github.com/SommerEngineering/Ocean/Shutdown" + "net/http" + "strings" +) + +// Handler for accessing the file upload function. +func HandlerConfiguration(response http.ResponseWriter, request *http.Request) { + + // Case: The system goes down now. + if Shutdown.IsDown() { + http.NotFound(response, request) + return + } + + if strings.ToLower(request.Method) == `get` { + // + // Case: Send the website to the client + // + + // Read all configuration values: + values := ConfigurationDB.ReadAll() + + // Build the data type for the template: + data := AdminWebConfiguration{} + data.Configuration = values + + // Write the MIME type and execute the template: + MimeTypes.Write2HTTP(response, MimeTypes.TypeWebHTML) + if executeError := AdminTemplates.ExecuteTemplate(response, `Configuration`, data); executeError != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameEXECUTE, `Was not able to execute the configuration template.`, executeError.Error()) + } + } else { + // + // Case: Receive the changed configuration + // + + // Read all configuration values: + values := ConfigurationDB.ReadAll() + + // Loop over all current known values: + for _, value := range values { + + // Read the new value from the client side: + newValue := request.FormValue(value.Name) + + // Store the new value: + value.Value = newValue + + // Update the database: + ConfigurationDB.UpdateValue(value.Name, value) + } + + // Redirect the client to the admin's overview: + defer http.Redirect(response, request, "/configuration", 302) + } + +} diff --git a/Admin/HandlerFileUpload.go b/Admin/HandlerFileUpload.go new file mode 100644 index 0000000..d545014 --- /dev/null +++ b/Admin/HandlerFileUpload.go @@ -0,0 +1,78 @@ +package Admin + +import ( + "fmt" + "github.com/SommerEngineering/Ocean/CustomerDB" + "github.com/SommerEngineering/Ocean/Log" + LM "github.com/SommerEngineering/Ocean/Log/Meta" + "github.com/SommerEngineering/Ocean/MimeTypes" + "github.com/SommerEngineering/Ocean/Shutdown" + "io" + "net/http" + "strings" +) + +// Handler for accessing the file upload function. +func HandlerFileUpload(response http.ResponseWriter, request *http.Request) { + + // Case: The system goes down now. + if Shutdown.IsDown() { + http.NotFound(response, request) + return + } + + if strings.ToLower(request.Method) == `get` { + // + // Case: Send the website to the client + // + + // Write the MIME type and execute the template: + MimeTypes.Write2HTTP(response, MimeTypes.TypeWebHTML) + if executeError := AdminTemplates.ExecuteTemplate(response, `FileUpload`, nil); executeError != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameEXECUTE, `Was not able to execute the file upload template.`, executeError.Error()) + } + } else { + // + // Case: Receive the file to upload + // + + if file, fileHeader, fileError := request.FormFile(`file`); fileError != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameBROWSER, `Was not able to access the file uploaded.`, fileError.Error()) + } else { + // + // Case: Access was possible. + // + + // Get the GridFS from the database: + dbSession, gridFS := CustomerDB.GridFS() + defer dbSession.Close() + + // Try to create the desired file at the grid file system: + if newFile, errNewFile := gridFS.Create(fileHeader.Filename); errNewFile != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameDATABASE, `Was not able to create the desired file at the grid file system.`, errNewFile.Error(), fmt.Sprintf("filename='%s'", fileHeader.Filename)) + } else { + + // Close the files afterwards: + defer file.Close() + defer newFile.Close() + + // Try to copy the file's content to the database: + if _, errCopy := io.Copy(newFile, file); errCopy != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactCritical, LM.MessageNameNETWORK, `Was not able to copy the desired file's content to the grid file system.`, errNewFile.Error(), fmt.Sprintf("filename='%s'", fileHeader.Filename)) + } else { + // Try to determine the MIME type: + if mimeType, errMime := MimeTypes.DetectType(fileHeader.Filename); errMime != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityLow, LM.ImpactLow, LM.MessageNamePARSE, `Was not able to parse the desired file's MIME type.`, errMime.Error(), fmt.Sprintf("filename='%s'", fileHeader.Filename)) + } else { + // Set also the MIME type in the database: + newFile.SetContentType(mimeType.MimeType) + } + } + } + } + + // Redirect the client to the admin's overview: + defer http.Redirect(response, request, "/", 302) + } + +} diff --git a/Admin/Init.go b/Admin/Init.go index 0d925d4..037bff6 100644 --- a/Admin/Init.go +++ b/Admin/Init.go @@ -22,4 +22,12 @@ func init() { if _, err := AdminTemplates.Parse(Templates.Overview); err != nil { Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactUnknown, LM.MessageNamePARSE, `Was not able to parse the template for the admin overview.`, err.Error()) } + + if _, err := AdminTemplates.Parse(Templates.FileUpload); err != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactUnknown, LM.MessageNamePARSE, `Was not able to parse the template for the file upload.`, err.Error()) + } + + if _, err := AdminTemplates.Parse(Templates.Configuration); err != nil { + Log.LogFull(senderName, LM.CategorySYSTEM, LM.LevelERROR, LM.SeverityCritical, LM.ImpactUnknown, LM.MessageNamePARSE, `Was not able to parse the template for the configuration.`, err.Error()) + } } diff --git a/Admin/Scheme.go b/Admin/Scheme.go new file mode 100644 index 0000000..f6208ab --- /dev/null +++ b/Admin/Scheme.go @@ -0,0 +1,9 @@ +package Admin + +import ( + "github.com/SommerEngineering/Ocean/ConfigurationDB" +) + +type AdminWebConfiguration struct { + Configuration []ConfigurationDB.ConfigurationDBEntry +} diff --git a/Admin/Templates/Configuration.go b/Admin/Templates/Configuration.go new file mode 100644 index 0000000..b023403 --- /dev/null +++ b/Admin/Templates/Configuration.go @@ -0,0 +1,47 @@ +package Templates + +var Configuration = ` +{{define "Configuration"}} + + + + +
+ +Attention: This configuration applies to the whole cluster of your Ocean servers. Therefore, this is not an individual configuration for any single server! Thus, please consider beforehand if the desired change matches all of your servers. +
+Thank you! Your submission has been received!
+Oops! Something went wrong while submitting the form
+This function enables you to upload a file to the distributed file system of the MongoDB database. If the desired file is already present, a new revision of this file is created. Therefore, an already existing file gets never overwritten! Please consider, that the configured maximum size of the header of the admin web server (see configuration) forces the maximum file size. Thus, please check the current maximum. The default maximum is approx. 10 MB!
+Thank you! Your submission has been received!
+Oops! Something went wrong while submitting the form
+