-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
217 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package vfsafero | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"os" | ||
|
||
"github.com/cozy/cozy-stack/model/vfs" | ||
"github.com/spf13/afero" | ||
) | ||
|
||
// NewAvatarFs creates a new avatar filesystem base on a afero.Fs. | ||
func NewAvatarFs(fs afero.Fs) vfs.Avatarer { | ||
return &avatar{fs} | ||
} | ||
|
||
type avatar struct { | ||
fs afero.Fs | ||
} | ||
|
||
type avatarUpload struct { | ||
afero.File | ||
fs afero.Fs | ||
tmpname string | ||
} | ||
|
||
func (u *avatarUpload) Close() error { | ||
if err := u.File.Close(); err != nil { | ||
_ = u.fs.Remove(u.tmpname) | ||
return err | ||
} | ||
return u.fs.Rename(u.tmpname, "avatar") | ||
} | ||
|
||
func (a *avatar) CreateAvatar(contentType string) (io.WriteCloser, error) { | ||
f, err := afero.TempFile(a.fs, "/", "avatar") | ||
if err != nil { | ||
return nil, err | ||
} | ||
tmpname := f.Name() | ||
u := &avatarUpload{ | ||
File: f, | ||
fs: a.fs, | ||
tmpname: tmpname, | ||
} | ||
return u, nil | ||
} | ||
|
||
func (a *avatar) AvatarExists() (bool, error) { | ||
infos, err := a.fs.Stat("avatar") | ||
if os.IsNotExist(err) { | ||
return false, nil | ||
} | ||
if err != nil { | ||
return false, err | ||
} | ||
return infos.Size() > 0, nil | ||
} | ||
|
||
func (a *avatar) ServeAvatarContent(w http.ResponseWriter, req *http.Request) error { | ||
s, err := a.fs.Stat("avatar") | ||
if err != nil { | ||
return err | ||
} | ||
if s.Size() == 0 { | ||
return os.ErrInvalid | ||
} | ||
f, err := a.fs.Open("avatar") | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
http.ServeContent(w, req, "avatar", s.ModTime(), f) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package vfsswift | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/cozy/cozy-stack/model/vfs" | ||
"github.com/cozy/cozy-stack/pkg/prefixer" | ||
"github.com/ncw/swift/v2" | ||
) | ||
|
||
// NewAvatarFsV3 creates a new avatar filesystem base on swift. | ||
// | ||
// This version stores the avatar in the same container as the main data | ||
// container. | ||
func NewAvatarFsV3(c *swift.Connection, db prefixer.Prefixer) vfs.Avatarer { | ||
return &avatarV3{ | ||
c: c, | ||
container: swiftV3ContainerPrefix + db.DBPrefix(), | ||
ctx: context.Background(), | ||
} | ||
} | ||
|
||
type avatarV3 struct { | ||
c *swift.Connection | ||
container string | ||
ctx context.Context | ||
} | ||
|
||
func (a *avatarV3) CreateAvatar(contentType string) (io.WriteCloser, error) { | ||
return a.c.ObjectCreate(a.ctx, a.container, "avatar", true, "", contentType, nil) | ||
} | ||
|
||
func (a *avatarV3) ServeAvatarContent(w http.ResponseWriter, req *http.Request) error { | ||
f, o, err := a.c.ObjectOpen(a.ctx, a.container, "avatar", false, nil) | ||
if err != nil { | ||
return wrapSwiftErr(err) | ||
} | ||
defer f.Close() | ||
|
||
w.Header().Set("Etag", fmt.Sprintf(`"%s"`, o["Etag"])) | ||
w.Header().Set("Content-Type", o["Content-Type"]) | ||
http.ServeContent(w, req, "avatar", unixEpochZero, &backgroundSeeker{f}) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters