Compare commits
4 commits
Author | SHA1 | Date | |
---|---|---|---|
a71b3697b6 | |||
530a1171e6 | |||
![]() |
3b6fa12911 | ||
9816e58e7c |
1 changed files with 19 additions and 10 deletions
29
context.go
29
context.go
|
@ -121,34 +121,43 @@ func (c *Context) GetQueueClient() *asynq.Client {
|
||||||
return 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)
|
file, fileHeader, err := c.Request.httpRequest.FormFile(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(fmt.Sprintf("error with file,[%v]", err.Error()))
|
return nil, fmt.Errorf("error retrieving file: %v", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
|
// Extract the file extension
|
||||||
ext := strings.TrimPrefix(path.Ext(fileHeader.Filename), ".")
|
ext := strings.TrimPrefix(path.Ext(fileHeader.Filename), ".")
|
||||||
tmpFilePath := filepath.Join(os.TempDir(), fileHeader.Filename)
|
tmpFilePath := filepath.Join(os.TempDir(), fileHeader.Filename)
|
||||||
tmpFile, err := os.Create(tmpFilePath)
|
tmpFile, err := os.Create(tmpFilePath)
|
||||||
if err != nil {
|
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)
|
buff := make([]byte, 100)
|
||||||
for {
|
for {
|
||||||
n, err := file.Read(buff)
|
n, err := file.Read(buff)
|
||||||
if err != nil && err != io.EOF {
|
if err != nil && err != io.EOF {
|
||||||
panic("error with uploaded file")
|
return nil, fmt.Errorf("error reading uploaded file: %v", err)
|
||||||
}
|
}
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
break
|
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)
|
tmpFileInfo, err := os.Stat(tmpFilePath)
|
||||||
if err != nil {
|
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{
|
uploadedFileInfo := &UploadedFileInfo{
|
||||||
FullPath: tmpFilePath,
|
FullPath: tmpFilePath,
|
||||||
Name: fileHeader.Filename,
|
Name: fileHeader.Filename,
|
||||||
|
@ -156,7 +165,7 @@ func (c *Context) GetUploadedFile(name string) *UploadedFileInfo {
|
||||||
Extension: ext,
|
Extension: ext,
|
||||||
Size: int(tmpFileInfo.Size()),
|
Size: int(tmpFileInfo.Size()),
|
||||||
}
|
}
|
||||||
return uploadedFileInfo
|
return uploadedFileInfo, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
|
func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
|
||||||
|
@ -185,7 +194,7 @@ func (c *Context) MoveFile(sourceFilePath string, destFolderPath string) error {
|
||||||
for {
|
for {
|
||||||
n, err := srcFile.Read(buff)
|
n, err := srcFile.Read(buff)
|
||||||
if err != nil && err != io.EOF {
|
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 {
|
if n == 0 {
|
||||||
break
|
break
|
||||||
|
@ -229,7 +238,7 @@ func (c *Context) CopyFile(sourceFilePath string, destFolderPath string) error {
|
||||||
for {
|
for {
|
||||||
n, err := srcFile.Read(buff)
|
n, err := srcFile.Read(buff)
|
||||||
if err != nil && err != io.EOF {
|
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 {
|
if n == 0 {
|
||||||
break
|
break
|
||||||
|
|
Loading…
Reference in a new issue