Skip to content

Commit 641ff2e

Browse files
author
Josh Cave
committed
fixes for new helpers
1 parent 21cc6c8 commit 641ff2e

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

fileupload.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,3 +339,34 @@ func (ip *imageProcessHelper) ProcessImage(filename string, bts []byte, ops *ope
339339
}
340340
return ip.fileSaver.SaveFile(filename, res.Body)
341341
}
342+
343+
type LocalFileStorage struct {
344+
AttachmentsFolder string
345+
AttachmentsFolderBaseURL string
346+
}
347+
348+
func NewLocalFileStorage(attachmentsFolder string, attachmentsFolderBaseURL string) *LocalFileStorage {
349+
return &LocalFileStorage{
350+
attachmentsFolder,
351+
attachmentsFolderBaseURL,
352+
}
353+
}
354+
355+
func (fs *LocalFileStorage) SaveFile(filename string, r io.Reader) (bts *bytes.Buffer, fileName string, url string, err error) {
356+
filename = getValidFileName(fs.AttachmentsFolder, filename)
357+
f, err := os.OpenFile(fs.AttachmentsFolder+filename, os.O_RDWR|os.O_CREATE, 0666)
358+
if err != nil {
359+
return nil, "", "", fmt.Errorf("Failed to create a file on the filesystem: %v", err)
360+
}
361+
defer f.Close()
362+
if err != nil {
363+
364+
return nil, "", "", fmt.Errorf("failed to get bytes from the original image: %v", err)
365+
}
366+
copy := io.TeeReader(r, f)
367+
bts, err = ioutil.ReadAll(copy)
368+
if err != nil {
369+
return nil, "", "", fmt.Errorf("failed to save the original image: %v", err)
370+
}
371+
return bytes.NewBuffer(bts), filename, fs.AttachmentsFolderBaseURL + filename, nil
372+
}

0 commit comments

Comments
 (0)