1
0
Fork 0
forked from goffee/core

Compare commits

...

8 commits
main ... main

5 changed files with 38 additions and 16 deletions

View file

@ -121,34 +121,43 @@ func (c *Context) GetQueueClient() *asynq.Client {
return client
}
func (c *Context) GetUploadedFile(name string) *UploadedFileInfo {
func (c *Context) GetUploadedFile(name string) (*UploadedFileInfo, error) {
file, fileHeader, err := c.Request.httpRequest.FormFile(name)
if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error()))
return nil, fmt.Errorf("error retrieving file: %v", err)
}
defer file.Close()
// Extract the file extension
ext := strings.TrimPrefix(path.Ext(fileHeader.Filename), ".")
tmpFilePath := filepath.Join(os.TempDir(), fileHeader.Filename)
tmpFile, err := os.Create(tmpFilePath)
if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error()))
return nil, fmt.Errorf("error creating temporary file: %v", err)
}
defer tmpFile.Close()
// Copy the uploaded file content to the temporary file
buff := make([]byte, 100)
for {
n, err := file.Read(buff)
if err != nil && err != io.EOF {
panic("error with uploaded file")
return nil, fmt.Errorf("error reading uploaded file: %v", err)
}
if n == 0 {
break
}
n, _ = tmpFile.Write(buff[:n])
_, err = tmpFile.Write(buff[:n])
if err != nil {
return nil, fmt.Errorf("error writing to temporary file: %v", err)
}
}
// Get file info for the temporary file
tmpFileInfo, err := os.Stat(tmpFilePath)
if err != nil {
panic(fmt.Sprintf("error with file,[%v]", err.Error()))
return nil, fmt.Errorf("error getting file info: %v", err)
}
defer tmpFile.Close()
uploadedFileInfo := &UploadedFileInfo{
FullPath: tmpFilePath,
Name: fileHeader.Filename,
@ -156,7 +165,7 @@ func (c *Context) GetUploadedFile(name string) *UploadedFileInfo {
Extension: ext,
Size: int(tmpFileInfo.Size()),
}
return uploadedFileInfo
return uploadedFileInfo, nil
}
func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
@ -185,7 +194,7 @@ func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
for {
n, err := srcFile.Read(buff)
if err != nil && err != io.EOF {
panic(fmt.Sprintf("error moving file %v", sourceFilePath))
return fmt.Errorf("error moving file: %v", sourceFilePath)
}
if n == 0 {
break
@ -229,7 +238,7 @@ func (c *Context) CopyFile(sourceFilePath string, destFolderPath string) error {
for {
n, err := srcFile.Read(buff)
if err != nil && err != io.EOF {
panic(fmt.Sprintf("error moving file %v", sourceFilePath))
return fmt.Errorf("error moving file: %v", sourceFilePath)
}
if n == 0 {
break

View file

@ -104,7 +104,7 @@ func (app *App) Run(router *httprouter.Router) {
// check if template engine is enable
TemplateEnableStr := os.Getenv("TEMPLATE_ENABLE")
if TemplateEnableStr == "" {
if "" == TemplateEnableStr {
TemplateEnableStr = "false"
}
TemplateEnable, _ := strconv.ParseBool(TemplateEnableStr)
@ -119,7 +119,7 @@ func (app *App) Run(router *httprouter.Router) {
}
useHttps, _ := strconv.ParseBool(useHttpsStr)
if runMode == "dev" {
if "dev" == runMode {
fmt.Printf("Welcome to Goffee\n")
if useHttps {
fmt.Printf("Listening on https \nWaiting for requests...\n")

View file

@ -40,6 +40,16 @@ func (rs *Response) BufferFile(name string, filetype string, b bytes.Buffer) *Re
return rs
}
// writes the contents of a buffer to the HTTP response with specified file name and type if not terminated.
func (rs *Response) BufferInline(name string, filetype string, b bytes.Buffer) *Response {
if rs.isTerminated == false {
rs.HttpResponseWriter.Header().Add("Content-Type", filetype)
b.WriteTo(rs.HttpResponseWriter)
}
return rs
}
// sets the response's content type to HTML and assigns the provided body as the response body if not terminated.
func (rs *Response) Any(body any) *Response {
if rs.isTerminated == false {

View file

@ -15,6 +15,7 @@ type ContentTableTH struct {
}
type ContentTableTD struct {
ID string
Value interface{} // string or component struct according ValueType
ID string
Value interface{} // string or component struct according ValueType
ValueClass string
}

View file

@ -7,7 +7,7 @@
</thead>
<tbody>
{{- range .AllTD}}<tr scope="row">
{{range $index, $item := .}}<td {{ if $item.ID }}id="{{$item.ID}}"{{end}}>
{{range $index, $item := .}}<td {{ if $item.ID }}id="{{$item.ID}}"{{end}}{{ if $item.ValueClass }} class="{{$item.ValueClass}}"{{end}}>
{{ with $x := index $.AllTH $index }}
{{ if eq $x.ValueType "href"}}
{{template "content_href" $item.Value}}
@ -16,7 +16,9 @@
{{ else if eq $x.ValueType "list"}}
{{template "content_list" $item.Value}}
{{ else if eq $x.ValueType "checkbox"}}
{{template "form_checkbox" $item.Value}}
{{template "form_checkbox" $item.Value}}
{{ else if eq $x.ValueType "image"}}
<img src="{{ $item.Value }}">
{{ else }}
{{ $item.Value }}
{{end}}