Skip to content

Commit

Permalink
[UPDATE] check if uniqeName is true to generate name & readme doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas-ggd committed Dec 15, 2024
1 parent cd5a5ac commit d81bca6
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 14 deletions.
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,61 @@ Here’s how you can get involved:
```
make test
```
4. To start application run:
```
make start
```

## Usage
```go
import(
"github.com/gin-gonic/gin"
"github.com/Nicolas-ggd/filestream"
)

func Upload(c *gin.Context) {
// Retrieve the uploaded file
uploadFile, err := c.FormFile("file")
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to get file"})
return
}

// Open the uploaded file
file, err := uploadFile.Open()
if err != nil {
fmt.Println("Error opening uploaded file:", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to open uploaded file"})
return
}

defer file.Close()


// Construct the FileRequest
fileReq := fstream.RFileRequest{
File: file,
UploadFile: uploadFile,
MaxRange: rangeMax,
FileSize: fileSize,
UploadDirectory: "uploads/",
FileUniqueName: true, // remember that if you set true here, you will receive unique name file
}


// Call StoreChunk to handle the uploaded chunk
prFile, err := fstream.StoreChunk(&fileReq)
if err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": "failed to store chunk"})
return
}

// You can write your own if statement to check whatever you want
if isLast {
// You can perform your own logic here, before return 200 status,
// it's better to remove chunks which is uploaded and doesn't use anymore
fstream.RemoveUploadedFile(&fileReq)
}

c.JSON(http.StatusOK, gin.H{"message": "file chunk processed"})
}
```

## License
FileStream is open-source software licensed under the MIT License.
Expand Down
25 changes: 15 additions & 10 deletions file.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type File struct {
// Original uploaded file name
FileName string
// FileUniqueName is unique name
FileUniqueName string
FileUniqueName *string
// Uploaded file path
FilePath string
// Uploaded file extension
Expand All @@ -43,16 +43,16 @@ type RFileRequest struct {
FileUniqueName bool
}

// generateUuidUniqueName function generates unique string using UUID
func (r *File) generateUuidUniqueName(request *RFileRequest) {
// uniqueName function generates unique string using UUID
func uniqueName(request *RFileRequest) string {
ext := filepath.Ext(request.UploadFile.Filename)

id, err := uuid.NewUUID()
if err != nil {
log.Fatalln(err)
}

r.FileUniqueName = fmt.Sprintf("%s%s", id.String(), ext)
return fmt.Sprintf("%s%s", id.String(), ext)
}

// RemoveUploadedFile function removes uploaded file from uploaded directory, it takes param and returns nothing:
Expand Down Expand Up @@ -118,7 +118,7 @@ func StoreChunk(r *RFileRequest) (*File, error) {
}()

// Copy the chunk data to the file
if _, err := io.Copy(f, r.File); err != nil {
if _, err = io.Copy(f, r.File); err != nil {
return nil, fmt.Errorf("failed to copying file: %v", err)
}

Expand All @@ -132,13 +132,18 @@ func StoreChunk(r *RFileRequest) (*File, error) {
// Calculate file size in bytes
size := prettyByteSize(int(fileInfo.Size()))

// Check if FileUniqueName field is true to generate unique name for file
if r.FileUniqueName {
uName := uniqueName(r)
rFile.FileUniqueName = &uName
}

// Bind File struct and return
rFile = &File{
FileName: r.UploadFile.Filename,
FileUniqueName: r.UploadFile.Filename,
FilePath: filePath,
FileExtension: filepath.Ext(r.UploadFile.Filename),
FileSize: size,
FileName: r.UploadFile.Filename,
FilePath: filePath,
FileExtension: filepath.Ext(r.UploadFile.Filename),
FileSize: size,
}
}

Expand Down
8 changes: 8 additions & 0 deletions file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fstream

import "testing"

func FuzzStoreChunk(f *testing.F) {
f.Fuzz(func(t *testing.T, data []byte) {
})
}
Binary file removed filestream
Binary file not shown.

0 comments on commit d81bca6

Please sign in to comment.