123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package diskcapacity
- import (
- "encoding/json"
- "net/http"
- "path/filepath"
- "imuslab.com/arozos/mod/common"
- "imuslab.com/arozos/mod/disk/diskcapacity/dftool"
- "imuslab.com/arozos/mod/filesystem"
- "imuslab.com/arozos/mod/user"
- )
- /*
- Disk Capacity
- This is a simple module to check how many storage space is remaining
- on a given directory in accessiable file system paths
- Author: tobychui
- */
- type Resolver struct {
- UserHandler *user.UserHandler
- }
- type CapacityInfo struct {
- PhysicalDevice string
- FileSystemType string
- MountingHierarchy string
- Used int64
- Available int64
- Total int64
- }
- //Create a new Capacity Resolver with the given user handler
- func NewCapacityResolver(u *user.UserHandler) *Resolver {
- return &Resolver{
- UserHandler: u,
- }
- }
- func (cr *Resolver) HandleCapacityResolving(w http.ResponseWriter, r *http.Request) {
- //Check if the request user is authenticated
- userinfo, err := cr.UserHandler.GetUserInfoFromRequest(w, r)
- if err != nil {
- common.SendErrorResponse(w, "User not logged in")
- return
- }
- //Get vpath from paramter
- vpath, err := common.Mv(r, "path", true)
- if err != nil {
- common.SendErrorResponse(w, "Vpath is not defined")
- return
- }
- capinfo, err := cr.ResolveCapacityInfo(userinfo.Username, vpath)
- if err != nil {
- common.SendErrorResponse(w, "Unable to resolve path capacity information: "+err.Error())
- return
- }
- //Get Storage Hierarcy
- fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
- if err != nil {
- capinfo.MountingHierarchy = "Unknown"
- } else {
- capinfo.MountingHierarchy = fsh.Hierarchy
- }
- //Send the requested path capacity information
- js, _ := json.Marshal(capinfo)
- common.SendJSONResponse(w, string(js))
- }
- func (cr *Resolver) ResolveCapacityInfo(username string, vpath string) (*CapacityInfo, error) {
- //Resolve the vpath for this user
- userinfo, err := cr.UserHandler.GetUserInfoFromUsername(username)
- if err != nil {
- return nil, err
- }
- fsh, err := userinfo.GetFileSystemHandlerFromVirtualPath(vpath)
- if err != nil {
- return nil, err
- }
- realpath, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(vpath, username)
- if err != nil {
- return nil, err
- }
- realpath = filepath.ToSlash(filepath.Clean(realpath))
- if filesystem.FileExists(realpath) && !fsh.RequireBuffer {
- //This is a local disk
- capinfo, err := dftool.GetCapacityInfoFromPath(realpath)
- if err != nil {
- return nil, err
- }
- return &CapacityInfo{
- PhysicalDevice: capinfo.PhysicalDevice,
- FileSystemType: fsh.Filesystem,
- MountingHierarchy: fsh.Hierarchy,
- Used: capinfo.Used,
- Available: capinfo.Available,
- Total: capinfo.Total,
- }, nil
- } else {
- //This is a remote disk
- return &CapacityInfo{
- PhysicalDevice: fsh.Path,
- FileSystemType: fsh.Filesystem,
- MountingHierarchy: fsh.Hierarchy,
- Used: 0,
- Available: 0,
- Total: 0,
- }, nil
- }
- }
|