github.com/avahowell/sia@v0.5.1-beta.0.20160524050156-83dcc3d37c94/api/renter.go (about)

     1  package api
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  
     8  	"github.com/NebulousLabs/Sia/build"
     9  	"github.com/NebulousLabs/Sia/modules"
    10  	"github.com/NebulousLabs/Sia/types"
    11  
    12  	"github.com/julienschmidt/httprouter"
    13  )
    14  
    15  var (
    16  	// TODO: Replace this function by accepting user input.
    17  	recommendedHosts = func() uint64 {
    18  		if build.Release == "dev" {
    19  			return 2
    20  		}
    21  		if build.Release == "standard" {
    22  			return 24
    23  		}
    24  		if build.Release == "testing" {
    25  			return 1
    26  		}
    27  		panic("unrecognized release constant in api")
    28  	}()
    29  )
    30  
    31  type (
    32  	// RenterGET contains various renter metrics.
    33  	RenterGET struct {
    34  		FinancialMetrics modules.RenterFinancialMetrics `json:"financialmetrics"`
    35  	}
    36  
    37  	// RenterContracts contains the renter's contracts.
    38  	RenterContracts struct {
    39  		Contracts []modules.RenterContract `json:"contracts"`
    40  	}
    41  
    42  	// DownloadQueue contains the renter's download queue.
    43  	RenterDownloadQueue struct {
    44  		Downloads []modules.DownloadInfo `json:"downloads"`
    45  	}
    46  
    47  	// RenterFiles lists the files known to the renter.
    48  	RenterFiles struct {
    49  		Files []modules.FileInfo `json:"files"`
    50  	}
    51  
    52  	// RenterLoad lists files that were loaded into the renter.
    53  	RenterLoad struct {
    54  		FilesAdded []string `json:"filesadded"`
    55  	}
    56  
    57  	// RenterShareASCII contains an ASCII-encoded .sia file.
    58  	RenterShareASCII struct {
    59  		ASCIIsia string `json:"asciisia"`
    60  	}
    61  
    62  	// ActiveHosts lists active hosts on the network.
    63  	ActiveHosts struct {
    64  		Hosts []modules.HostDBEntry `json:"hosts"`
    65  	}
    66  )
    67  
    68  // renterHandler handles the API call to /renter.
    69  func (srv *Server) renterHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    70  	writeJSON(w, RenterGET{
    71  		FinancialMetrics: srv.renter.FinancialMetrics(),
    72  	})
    73  }
    74  
    75  // renterAllowanceHandlerGET handles the API call to get the allowance.
    76  func (srv *Server) renterAllowanceHandlerGET(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    77  	writeJSON(w, srv.renter.Allowance())
    78  }
    79  
    80  // renterAllowanceHandlerPOST handles the API call to set the allowance.
    81  func (srv *Server) renterAllowanceHandlerPOST(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
    82  	// scan values
    83  	funds, ok := scanAmount(req.FormValue("funds"))
    84  	if !ok {
    85  		writeError(w, "Couldn't parse funds", http.StatusBadRequest)
    86  		return
    87  	}
    88  	// var hosts uint64
    89  	// _, err := fmt.Sscan(req.FormValue("hosts"), &hosts)
    90  	// if err != nil {
    91  	// 	writeError(w, "Couldn't parse hosts: "+err.Error(), http.StatusBadRequest)
    92  	// 	return
    93  	// }
    94  	var period types.BlockHeight
    95  	_, err := fmt.Sscan(req.FormValue("period"), &period)
    96  	if err != nil {
    97  		writeError(w, "Couldn't parse period: "+err.Error(), http.StatusBadRequest)
    98  		return
    99  	}
   100  	// var renewWindow types.BlockHeight
   101  	// _, err = fmt.Sscan(req.FormValue("renewwindow"), &renewWindow)
   102  	// if err != nil {
   103  	// 	writeError(w, "Couldn't parse renewwindow: "+err.Error(), http.StatusBadRequest)
   104  	// 	return
   105  	// }
   106  
   107  	err = srv.renter.SetAllowance(modules.Allowance{
   108  		Funds:  funds,
   109  		Period: period,
   110  
   111  		// TODO: let user specify these
   112  		Hosts:       recommendedHosts,
   113  		RenewWindow: period / 2,
   114  	})
   115  	if err != nil {
   116  		writeError(w, err.Error(), http.StatusBadRequest)
   117  		return
   118  	}
   119  	writeSuccess(w)
   120  }
   121  
   122  // renterContractsHandler handles the API call to request the Renter's contracts.
   123  func (srv *Server) renterContractsHandler(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
   124  	writeJSON(w, RenterContracts{
   125  		Contracts: srv.renter.Contracts(),
   126  	})
   127  }
   128  
   129  // renterDownloadsHandler handles the API call to request the download queue.
   130  func (srv *Server) renterDownloadsHandler(w http.ResponseWriter, _ *http.Request, _ httprouter.Params) {
   131  	writeJSON(w, RenterDownloadQueue{
   132  		Downloads: srv.renter.DownloadQueue(),
   133  	})
   134  }
   135  
   136  // renterLoadHandler handles the API call to load a '.sia' file.
   137  func (srv *Server) renterLoadHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   138  	files, err := srv.renter.LoadSharedFiles(req.FormValue("source"))
   139  	if err != nil {
   140  		writeError(w, err.Error(), http.StatusBadRequest)
   141  		return
   142  	}
   143  
   144  	writeJSON(w, RenterLoad{FilesAdded: files})
   145  }
   146  
   147  // renterLoadAsciiHandler handles the API call to load a '.sia' file
   148  // in ASCII form.
   149  func (srv *Server) renterLoadAsciiHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   150  	files, err := srv.renter.LoadSharedFilesAscii(req.FormValue("asciisia"))
   151  	if err != nil {
   152  		writeError(w, err.Error(), http.StatusBadRequest)
   153  		return
   154  	}
   155  
   156  	writeJSON(w, RenterLoad{FilesAdded: files})
   157  }
   158  
   159  // renterRenameHandler handles the API call to rename a file entry in the
   160  // renter.
   161  func (srv *Server) renterRenameHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   162  	err := srv.renter.RenameFile(strings.TrimPrefix(ps.ByName("siapath"), "/"), req.FormValue("newsiapath"))
   163  	if err != nil {
   164  		writeError(w, err.Error(), http.StatusBadRequest)
   165  		return
   166  	}
   167  
   168  	writeSuccess(w)
   169  }
   170  
   171  // renterFilesHandler handles the API call to list all of the files.
   172  func (srv *Server) renterFilesHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   173  	writeJSON(w, RenterFiles{
   174  		Files: srv.renter.FileList(),
   175  	})
   176  }
   177  
   178  // renterDeleteHander handles the API call to delete a file entry from the
   179  // renter.
   180  func (srv *Server) renterDeleteHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   181  	err := srv.renter.DeleteFile(strings.TrimPrefix(ps.ByName("siapath"), "/"))
   182  	if err != nil {
   183  		writeError(w, err.Error(), http.StatusBadRequest)
   184  		return
   185  	}
   186  
   187  	writeSuccess(w)
   188  }
   189  
   190  // renterDownloadHandler handles the API call to download a file.
   191  func (srv *Server) renterDownloadHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   192  	err := srv.renter.Download(strings.TrimPrefix(ps.ByName("siapath"), "/"), req.FormValue("destination"))
   193  	if err != nil {
   194  		writeError(w, "Download failed: "+err.Error(), http.StatusInternalServerError)
   195  		return
   196  	}
   197  
   198  	writeSuccess(w)
   199  }
   200  
   201  // renterShareHandler handles the API call to create a '.sia' file that
   202  // shares a set of file.
   203  func (srv *Server) renterShareHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   204  	err := srv.renter.ShareFiles(strings.Split(req.FormValue("siapaths"), ","), req.FormValue("destination"))
   205  	if err != nil {
   206  		writeError(w, err.Error(), http.StatusBadRequest)
   207  		return
   208  	}
   209  
   210  	writeSuccess(w)
   211  }
   212  
   213  // renterShareAsciiHandler handles the API call to return a '.sia' file
   214  // in ascii form.
   215  func (srv *Server) renterShareAsciiHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   216  	ascii, err := srv.renter.ShareFilesAscii(strings.Split(req.FormValue("siapaths"), ","))
   217  	if err != nil {
   218  		writeError(w, err.Error(), http.StatusBadRequest)
   219  		return
   220  	}
   221  	writeJSON(w, RenterShareASCII{
   222  		ASCIIsia: ascii,
   223  	})
   224  }
   225  
   226  // renterUploadHandler handles the API call to upload a file.
   227  func (srv *Server) renterUploadHandler(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
   228  	err := srv.renter.Upload(modules.FileUploadParams{
   229  		Source:  req.FormValue("source"),
   230  		SiaPath: strings.TrimPrefix(ps.ByName("siapath"), "/"),
   231  		// let the renter decide these values; eventually they will be configurable
   232  		ErasureCode: nil,
   233  	})
   234  	if err != nil {
   235  		writeError(w, "Upload failed: "+err.Error(), http.StatusInternalServerError)
   236  		return
   237  	}
   238  
   239  	writeSuccess(w)
   240  }
   241  
   242  // renterHostsActiveHandler handes the API call asking for the list of active
   243  // hosts.
   244  func (srv *Server) renterHostsActiveHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   245  	writeJSON(w, ActiveHosts{
   246  		Hosts: srv.renter.ActiveHosts(),
   247  	})
   248  }
   249  
   250  // renterHostsAllHandler handes the API call asking for the list of all hosts.
   251  func (srv *Server) renterHostsAllHandler(w http.ResponseWriter, req *http.Request, _ httprouter.Params) {
   252  	writeJSON(w, ActiveHosts{
   253  		Hosts: srv.renter.AllHosts(),
   254  	})
   255  }