2014-11-16 20:13:20 +00:00
|
|
|
// Copyright (C) 2014 The Syncthing Authors.
|
2014-09-29 19:43:32 +00:00
|
|
|
//
|
2015-03-07 20:36:35 +00:00
|
|
|
// This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
// You can obtain one at http://mozilla.org/MPL/2.0/.
|
2014-06-01 20:50:14 +00:00
|
|
|
|
2014-03-02 22:58:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2015-04-04 17:06:20 +00:00
|
|
|
"bytes"
|
|
|
|
"compress/gzip"
|
2014-08-21 22:45:40 +00:00
|
|
|
"crypto/tls"
|
2014-03-02 22:58:14 +00:00
|
|
|
"encoding/json"
|
2014-05-22 14:12:19 +00:00
|
|
|
"fmt"
|
2014-03-02 22:58:14 +00:00
|
|
|
"io/ioutil"
|
2014-05-22 14:12:19 +00:00
|
|
|
"mime"
|
2014-04-30 20:52:38 +00:00
|
|
|
"net"
|
2014-03-02 22:58:14 +00:00
|
|
|
"net/http"
|
2014-07-22 18:11:36 +00:00
|
|
|
"os"
|
2014-05-22 14:12:19 +00:00
|
|
|
"path/filepath"
|
2015-04-07 19:45:22 +00:00
|
|
|
"reflect"
|
2014-03-02 22:58:14 +00:00
|
|
|
"runtime"
|
2014-07-13 19:07:24 +00:00
|
|
|
"strconv"
|
2014-07-05 19:40:29 +00:00
|
|
|
"strings"
|
2014-03-02 22:58:14 +00:00
|
|
|
"time"
|
|
|
|
|
2014-10-26 12:15:14 +00:00
|
|
|
"github.com/calmh/logger"
|
2015-01-13 12:22:56 +00:00
|
|
|
"github.com/syncthing/protocol"
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/auto"
|
|
|
|
"github.com/syncthing/syncthing/internal/config"
|
2015-01-12 13:50:30 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/db"
|
2014-10-15 20:52:06 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/discover"
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/events"
|
|
|
|
"github.com/syncthing/syncthing/internal/model"
|
2014-10-06 07:25:45 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/osutil"
|
2015-04-22 22:54:31 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/sync"
|
2014-09-22 19:42:11 +00:00
|
|
|
"github.com/syncthing/syncthing/internal/upgrade"
|
2014-05-21 18:06:14 +00:00
|
|
|
"github.com/vitrun/qart/qr"
|
2014-11-29 23:17:00 +00:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type guiError struct {
|
2015-03-10 22:45:43 +00:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Error string `json:"error"`
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2015-04-28 20:32:10 +00:00
|
|
|
configInSync = true
|
|
|
|
guiErrors = []guiError{}
|
|
|
|
guiErrorsMut = sync.NewMutex()
|
|
|
|
startTime = time.Now()
|
2014-03-02 22:58:14 +00:00
|
|
|
)
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
type apiSvc struct {
|
2015-06-22 15:57:08 +00:00
|
|
|
id protocol.DeviceID
|
2015-06-03 07:47:39 +00:00
|
|
|
cfg config.GUIConfiguration
|
|
|
|
assetDir string
|
|
|
|
model *model.Model
|
|
|
|
listener net.Listener
|
|
|
|
fss *folderSummarySvc
|
|
|
|
stop chan struct{}
|
|
|
|
systemConfigMut sync.Mutex
|
2015-06-16 07:17:58 +00:00
|
|
|
eventSub *events.BufferedSubscription
|
2015-04-28 21:12:19 +00:00
|
|
|
}
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2015-06-22 15:57:08 +00:00
|
|
|
func newAPISvc(id protocol.DeviceID, cfg config.GUIConfiguration, assetDir string, m *model.Model, eventSub *events.BufferedSubscription) (*apiSvc, error) {
|
2015-04-28 21:12:19 +00:00
|
|
|
svc := &apiSvc{
|
2015-06-22 15:57:08 +00:00
|
|
|
id: id,
|
2015-06-03 07:47:39 +00:00
|
|
|
cfg: cfg,
|
|
|
|
assetDir: assetDir,
|
|
|
|
model: m,
|
|
|
|
systemConfigMut: sync.NewMutex(),
|
2015-06-16 07:17:58 +00:00
|
|
|
eventSub: eventSub,
|
2015-04-28 21:12:19 +00:00
|
|
|
}
|
2015-04-20 03:34:04 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
var err error
|
2015-06-03 07:47:39 +00:00
|
|
|
svc.listener, err = svc.getListener(cfg)
|
2015-04-28 21:12:19 +00:00
|
|
|
return svc, err
|
|
|
|
}
|
2014-09-12 19:28:47 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
func (s *apiSvc) getListener(cfg config.GUIConfiguration) (net.Listener, error) {
|
2015-04-04 19:59:54 +00:00
|
|
|
cert, err := tls.LoadX509KeyPair(locations[locHTTPSCertFile], locations[locHTTPSKeyFile])
|
2014-09-12 19:28:47 +00:00
|
|
|
if err != nil {
|
|
|
|
l.Infoln("Loading HTTPS certificate:", err)
|
|
|
|
l.Infoln("Creating new HTTPS certificate")
|
2014-12-09 09:42:56 +00:00
|
|
|
|
|
|
|
// When generating the HTTPS certificate, use the system host name per
|
|
|
|
// default. If that isn't available, use the "syncthing" default.
|
2014-12-16 21:55:44 +00:00
|
|
|
var name string
|
|
|
|
name, err = os.Hostname()
|
2014-12-09 09:42:56 +00:00
|
|
|
if err != nil {
|
|
|
|
name = tlsDefaultCommonName
|
|
|
|
}
|
|
|
|
|
2015-04-04 19:59:54 +00:00
|
|
|
cert, err = newCertificate(locations[locHTTPSCertFile], locations[locHTTPSKeyFile], name)
|
2014-09-12 19:28:47 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
2015-04-28 21:12:19 +00:00
|
|
|
return nil, err
|
2014-09-12 19:28:47 +00:00
|
|
|
}
|
|
|
|
tlsCfg := &tls.Config{
|
|
|
|
Certificates: []tls.Certificate{cert},
|
2014-12-09 09:42:56 +00:00
|
|
|
MinVersion: tls.VersionTLS10, // No SSLv3
|
|
|
|
CipherSuites: []uint16{
|
|
|
|
// No RC4
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_128_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_AES_256_CBC_SHA,
|
|
|
|
tls.TLS_ECDHE_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
|
|
tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA,
|
|
|
|
},
|
2014-04-30 20:52:38 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
rawListener, err := net.Listen("tcp", cfg.Address)
|
2014-09-12 19:28:47 +00:00
|
|
|
if err != nil {
|
2015-04-28 21:12:19 +00:00
|
|
|
return nil, err
|
2014-04-30 20:52:38 +00:00
|
|
|
}
|
2015-04-28 21:12:19 +00:00
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
listener := &DowngradingListener{rawListener, tlsCfg}
|
2015-04-28 21:12:19 +00:00
|
|
|
return listener, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiSvc) Serve() {
|
2015-06-03 07:47:39 +00:00
|
|
|
s.stop = make(chan struct{})
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
l.AddHandler(logger.LevelWarn, s.showGuiError)
|
2014-04-30 20:52:38 +00:00
|
|
|
|
2014-07-05 19:40:29 +00:00
|
|
|
// The GET handlers
|
|
|
|
getRestMux := http.NewServeMux()
|
2015-04-28 21:12:19 +00:00
|
|
|
getRestMux.HandleFunc("/rest/db/completion", s.getDBCompletion) // device folder
|
|
|
|
getRestMux.HandleFunc("/rest/db/file", s.getDBFile) // folder file
|
|
|
|
getRestMux.HandleFunc("/rest/db/ignores", s.getDBIgnores) // folder
|
|
|
|
getRestMux.HandleFunc("/rest/db/need", s.getDBNeed) // folder [perpage] [page]
|
|
|
|
getRestMux.HandleFunc("/rest/db/status", s.getDBStatus) // folder
|
|
|
|
getRestMux.HandleFunc("/rest/db/browse", s.getDBBrowse) // folder [prefix] [dirsonly] [levels]
|
|
|
|
getRestMux.HandleFunc("/rest/events", s.getEvents) // since [limit]
|
|
|
|
getRestMux.HandleFunc("/rest/stats/device", s.getDeviceStats) // -
|
|
|
|
getRestMux.HandleFunc("/rest/stats/folder", s.getFolderStats) // -
|
|
|
|
getRestMux.HandleFunc("/rest/svc/deviceid", s.getDeviceID) // id
|
|
|
|
getRestMux.HandleFunc("/rest/svc/lang", s.getLang) // -
|
|
|
|
getRestMux.HandleFunc("/rest/svc/report", s.getReport) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/browse", s.getSystemBrowse) // current
|
|
|
|
getRestMux.HandleFunc("/rest/system/config", s.getSystemConfig) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/config/insync", s.getSystemConfigInsync) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/connections", s.getSystemConnections) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/discovery", s.getSystemDiscovery) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/error", s.getSystemError) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/ping", s.restPing) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/status", s.getSystemStatus) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/upgrade", s.getSystemUpgrade) // -
|
|
|
|
getRestMux.HandleFunc("/rest/system/version", s.getSystemVersion) // -
|
2014-07-29 11:01:27 +00:00
|
|
|
|
2014-07-05 19:40:29 +00:00
|
|
|
// The POST handlers
|
|
|
|
postRestMux := http.NewServeMux()
|
2015-04-28 21:12:19 +00:00
|
|
|
postRestMux.HandleFunc("/rest/db/prio", s.postDBPrio) // folder file [perpage] [page]
|
|
|
|
postRestMux.HandleFunc("/rest/db/ignores", s.postDBIgnores) // folder
|
|
|
|
postRestMux.HandleFunc("/rest/db/override", s.postDBOverride) // folder
|
2015-05-01 12:30:17 +00:00
|
|
|
postRestMux.HandleFunc("/rest/db/scan", s.postDBScan) // folder [sub...] [delay]
|
2015-04-28 21:12:19 +00:00
|
|
|
postRestMux.HandleFunc("/rest/system/config", s.postSystemConfig) // <body>
|
|
|
|
postRestMux.HandleFunc("/rest/system/discovery", s.postSystemDiscovery) // device addr
|
|
|
|
postRestMux.HandleFunc("/rest/system/error", s.postSystemError) // <body>
|
|
|
|
postRestMux.HandleFunc("/rest/system/error/clear", s.postSystemErrorClear) // -
|
|
|
|
postRestMux.HandleFunc("/rest/system/ping", s.restPing) // -
|
|
|
|
postRestMux.HandleFunc("/rest/system/reset", s.postSystemReset) // [folder]
|
|
|
|
postRestMux.HandleFunc("/rest/system/restart", s.postSystemRestart) // -
|
|
|
|
postRestMux.HandleFunc("/rest/system/shutdown", s.postSystemShutdown) // -
|
|
|
|
postRestMux.HandleFunc("/rest/system/upgrade", s.postSystemUpgrade) // -
|
2015-04-06 08:23:27 +00:00
|
|
|
|
|
|
|
// Debug endpoints, not for general use
|
2015-04-28 21:12:19 +00:00
|
|
|
getRestMux.HandleFunc("/rest/debug/peerCompletion", s.getPeerCompletion)
|
2014-07-05 19:40:29 +00:00
|
|
|
|
|
|
|
// A handler that splits requests between the two above and disables
|
|
|
|
// caching
|
|
|
|
restMux := noCacheMiddleware(getPostHandler(getRestMux, postRestMux))
|
|
|
|
|
|
|
|
// The main routing handler
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
mux.Handle("/rest/", restMux)
|
2015-04-28 21:12:19 +00:00
|
|
|
mux.HandleFunc("/qr/", s.getQR)
|
2014-07-05 19:40:29 +00:00
|
|
|
|
|
|
|
// Serve compiled in assets unless an asset directory was set (for development)
|
2015-04-28 21:12:19 +00:00
|
|
|
mux.Handle("/", embeddedStatic{
|
|
|
|
assetDir: s.assetDir,
|
|
|
|
assets: auto.Assets(),
|
|
|
|
})
|
2014-05-22 14:12:19 +00:00
|
|
|
|
2014-07-06 13:00:44 +00:00
|
|
|
// Wrap everything in CSRF protection. The /rest prefix should be
|
|
|
|
// protected, other requests will grant cookies.
|
2015-06-22 15:57:08 +00:00
|
|
|
handler := csrfMiddleware(s.id.String()[:5], "/rest", s.cfg.APIKey, mux)
|
2014-06-04 19:20:07 +00:00
|
|
|
|
2015-06-22 15:57:08 +00:00
|
|
|
// Add our version and ID as a header to responses
|
|
|
|
handler = withDetailsMiddleware(s.id, handler)
|
2014-08-31 10:59:20 +00:00
|
|
|
|
2014-07-05 19:40:29 +00:00
|
|
|
// Wrap everything in basic auth, if user/password is set.
|
2015-04-28 21:12:19 +00:00
|
|
|
if len(s.cfg.User) > 0 && len(s.cfg.Password) > 0 {
|
2015-06-22 15:57:08 +00:00
|
|
|
handler = basicAuthAndSessionMiddleware("sessionid-"+s.id.String()[:5], s.cfg, handler)
|
2014-07-05 19:40:29 +00:00
|
|
|
}
|
2014-04-30 20:52:38 +00:00
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
// Redirect to HTTPS if we are supposed to
|
2015-04-28 21:12:19 +00:00
|
|
|
if s.cfg.UseTLS {
|
2014-09-14 22:18:05 +00:00
|
|
|
handler = redirectToHTTPSMiddleware(handler)
|
|
|
|
}
|
2014-09-12 19:28:47 +00:00
|
|
|
|
2015-04-07 19:45:22 +00:00
|
|
|
if debugHTTP {
|
|
|
|
handler = debugMiddleware(handler)
|
|
|
|
}
|
|
|
|
|
2014-10-13 17:34:26 +00:00
|
|
|
srv := http.Server{
|
|
|
|
Handler: handler,
|
2014-11-30 09:35:04 +00:00
|
|
|
ReadTimeout: 10 * time.Second,
|
2014-10-13 17:34:26 +00:00
|
|
|
}
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
s.fss = newFolderSummarySvc(s.model)
|
|
|
|
defer s.fss.Stop()
|
2015-04-28 21:12:19 +00:00
|
|
|
s.fss.ServeBackground()
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
l.Infoln("API listening on", s.listener.Addr())
|
2015-04-28 21:12:19 +00:00
|
|
|
err := srv.Serve(s.listener)
|
2015-06-03 07:47:39 +00:00
|
|
|
|
|
|
|
// The return could be due to an intentional close. Wait for the stop
|
|
|
|
// signal before returning. IF there is no stop signal within a second, we
|
|
|
|
// assume it was unintentional and log the error before retrying.
|
|
|
|
select {
|
|
|
|
case <-s.stop:
|
|
|
|
case <-time.After(time.Second):
|
|
|
|
l.Warnln("API:", err)
|
|
|
|
}
|
2015-04-28 21:12:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiSvc) Stop() {
|
2015-06-03 07:47:39 +00:00
|
|
|
close(s.stop)
|
|
|
|
s.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiSvc) String() string {
|
|
|
|
return fmt.Sprintf("apiSvc@%p", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiSvc) VerifyConfiguration(from, to config.Configuration) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *apiSvc) CommitConfiguration(from, to config.Configuration) bool {
|
|
|
|
if to.GUI == from.GUI {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order here is important. We must close the listener to stop Serve(). We
|
|
|
|
// must create a new listener before Serve() starts again. We can't create
|
|
|
|
// a new listener on the same port before the previous listener is closed.
|
|
|
|
// To assist in this little dance the Serve() method will wait for a
|
|
|
|
// signal on the stop channel after the listener has closed.
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
s.listener.Close()
|
2015-06-03 07:47:39 +00:00
|
|
|
|
|
|
|
var err error
|
|
|
|
s.listener, err = s.getListener(to.GUI)
|
|
|
|
if err != nil {
|
|
|
|
// Ideally this should be a verification error, but we check it by
|
|
|
|
// creating a new listener which requires shutting down the previous
|
|
|
|
// one first, which is too destructive for the VerifyConfiguration
|
|
|
|
// method.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
s.cfg = to.GUI
|
|
|
|
|
|
|
|
close(s.stop)
|
|
|
|
|
|
|
|
return true
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2014-07-05 19:40:29 +00:00
|
|
|
func getPostHandler(get, post http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
switch r.Method {
|
|
|
|
case "GET":
|
|
|
|
get.ServeHTTP(w, r)
|
|
|
|
case "POST":
|
|
|
|
post.ServeHTTP(w, r)
|
|
|
|
default:
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
|
|
}
|
|
|
|
})
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-07 19:45:22 +00:00
|
|
|
func debugMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
t0 := time.Now()
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
ms := 1000 * time.Since(t0).Seconds()
|
|
|
|
|
|
|
|
// The variable `w` is most likely a *http.response, which we can't do
|
|
|
|
// much with since it's a non exported type. We can however peek into
|
|
|
|
// it with reflection to get at the status code and number of bytes
|
|
|
|
// written.
|
|
|
|
var status, written int64
|
|
|
|
if rw := reflect.Indirect(reflect.ValueOf(w)); rw.IsValid() && rw.Kind() == reflect.Struct {
|
|
|
|
if rf := rw.FieldByName("status"); rf.IsValid() && rf.Kind() == reflect.Int {
|
|
|
|
status = rf.Int()
|
|
|
|
}
|
|
|
|
if rf := rw.FieldByName("written"); rf.IsValid() && rf.Kind() == reflect.Int64 {
|
|
|
|
written = rf.Int()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
l.Debugf("http: %s %q: status %d, %d bytes in %.02f ms", r.Method, r.URL.String(), status, written, ms)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-09-14 22:18:05 +00:00
|
|
|
func redirectToHTTPSMiddleware(h http.Handler) http.Handler {
|
2014-09-12 19:28:47 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2014-09-14 22:18:05 +00:00
|
|
|
// Add a generous access-control-allow-origin header since we may be
|
|
|
|
// redirecting REST requests over protocols
|
|
|
|
w.Header().Add("Access-Control-Allow-Origin", "*")
|
|
|
|
|
|
|
|
if r.TLS == nil {
|
|
|
|
// Redirect HTTP requests to HTTPS
|
|
|
|
r.URL.Host = r.Host
|
2014-09-12 19:28:47 +00:00
|
|
|
r.URL.Scheme = "https"
|
|
|
|
http.Redirect(w, r, r.URL.String(), http.StatusFound)
|
|
|
|
} else {
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2014-07-05 19:40:29 +00:00
|
|
|
func noCacheMiddleware(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2015-04-27 07:08:55 +00:00
|
|
|
w.Header().Set("Cache-Control", "max-age=0, no-cache, no-store")
|
|
|
|
w.Header().Set("Expires", time.Now().UTC().Format(http.TimeFormat))
|
|
|
|
w.Header().Set("Pragma", "no-cache")
|
2014-07-05 19:40:29 +00:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-06-22 15:57:08 +00:00
|
|
|
func withDetailsMiddleware(id protocol.DeviceID, h http.Handler) http.Handler {
|
2014-08-31 10:59:20 +00:00
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
w.Header().Set("X-Syncthing-Version", Version)
|
2015-06-22 15:57:08 +00:00
|
|
|
w.Header().Set("X-Syncthing-ID", id.String())
|
2014-08-31 10:59:20 +00:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) restPing(w http.ResponseWriter, r *http.Request) {
|
2014-09-18 10:55:28 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"ping": "pong",
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemVersion(w http.ResponseWriter, r *http.Request) {
|
2014-09-18 10:52:45 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"version": Version,
|
|
|
|
"longVersion": LongVersion,
|
|
|
|
"os": runtime.GOOS,
|
|
|
|
"arch": runtime.GOARCH,
|
|
|
|
})
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBBrowse(w http.ResponseWriter, r *http.Request) {
|
2015-02-07 10:52:42 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
folder := qs.Get("folder")
|
|
|
|
prefix := qs.Get("prefix")
|
|
|
|
dirsonly := qs.Get("dirsonly") != ""
|
|
|
|
|
|
|
|
levels, err := strconv.Atoi(qs.Get("levels"))
|
|
|
|
if err != nil {
|
|
|
|
levels = -1
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
tree := s.model.GlobalDirectoryTree(folder, prefix, levels, dirsonly)
|
2015-02-07 10:52:42 +00:00
|
|
|
|
|
|
|
json.NewEncoder(w).Encode(tree)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBCompletion(w http.ResponseWriter, r *http.Request) {
|
2014-07-29 09:06:52 +00:00
|
|
|
var qs = r.URL.Query()
|
2014-09-28 11:00:38 +00:00
|
|
|
var folder = qs.Get("folder")
|
|
|
|
var deviceStr = qs.Get("device")
|
2014-07-29 09:06:52 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
device, err := protocol.DeviceIDFromString(deviceStr)
|
2014-07-29 09:06:52 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
res := map[string]float64{
|
2015-04-28 21:12:19 +00:00
|
|
|
"completion": s.model.Completion(device, folder),
|
2014-07-29 09:06:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBStatus(w http.ResponseWriter, r *http.Request) {
|
2015-03-26 22:26:51 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
folder := qs.Get("folder")
|
2015-04-28 21:12:19 +00:00
|
|
|
res := folderSummary(s.model, folder)
|
2015-03-26 22:26:51 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
|
|
|
func folderSummary(m *model.Model, folder string) map[string]interface{} {
|
2014-03-02 22:58:14 +00:00
|
|
|
var res = make(map[string]interface{})
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
res["invalid"] = cfg.Folders()[folder].Invalid
|
2014-04-27 19:53:27 +00:00
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
globalFiles, globalDeleted, globalBytes := m.GlobalSize(folder)
|
2014-03-02 22:58:14 +00:00
|
|
|
res["globalFiles"], res["globalDeleted"], res["globalBytes"] = globalFiles, globalDeleted, globalBytes
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
localFiles, localDeleted, localBytes := m.LocalSize(folder)
|
2014-03-02 22:58:14 +00:00
|
|
|
res["localFiles"], res["localDeleted"], res["localBytes"] = localFiles, localDeleted, localBytes
|
|
|
|
|
2014-09-28 11:00:38 +00:00
|
|
|
needFiles, needBytes := m.NeedSize(folder)
|
2014-04-09 20:03:30 +00:00
|
|
|
res["needFiles"], res["needBytes"] = needFiles, needBytes
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2014-04-09 20:03:30 +00:00
|
|
|
res["inSyncFiles"], res["inSyncBytes"] = globalFiles-needFiles, globalBytes-needBytes
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2015-04-12 20:12:01 +00:00
|
|
|
var err error
|
|
|
|
res["state"], res["stateChanged"], err = m.State(folder)
|
|
|
|
if err != nil {
|
|
|
|
res["error"] = err.Error()
|
|
|
|
}
|
|
|
|
|
2015-06-24 07:52:38 +00:00
|
|
|
lv, _ := m.CurrentLocalVersion(folder)
|
|
|
|
rv, _ := m.RemoteLocalVersion(folder)
|
|
|
|
|
|
|
|
res["version"] = lv + rv
|
2014-04-14 07:58:17 +00:00
|
|
|
|
2015-01-27 14:27:44 +00:00
|
|
|
ignorePatterns, _, _ := m.GetIgnores(folder)
|
|
|
|
res["ignorePatterns"] = false
|
|
|
|
for _, line := range ignorePatterns {
|
|
|
|
if len(line) > 0 && !strings.HasPrefix(line, "//") {
|
|
|
|
res["ignorePatterns"] = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-26 22:26:51 +00:00
|
|
|
return res
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postDBOverride(w http.ResponseWriter, r *http.Request) {
|
2014-06-16 08:47:02 +00:00
|
|
|
var qs = r.URL.Query()
|
2014-09-28 11:00:38 +00:00
|
|
|
var folder = qs.Get("folder")
|
2015-04-28 21:12:19 +00:00
|
|
|
go s.model.Override(folder)
|
2014-06-16 08:47:02 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBNeed(w http.ResponseWriter, r *http.Request) {
|
2015-04-25 21:53:44 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
|
|
|
|
folder := qs.Get("folder")
|
|
|
|
|
|
|
|
page, err := strconv.Atoi(qs.Get("page"))
|
|
|
|
if err != nil || page < 1 {
|
|
|
|
page = 1
|
|
|
|
}
|
|
|
|
perpage, err := strconv.Atoi(qs.Get("perpage"))
|
|
|
|
if err != nil || perpage < 1 {
|
|
|
|
perpage = 1 << 16
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
progress, queued, rest, total := s.model.NeedFolderFiles(folder, page, perpage)
|
2014-05-19 20:31:28 +00:00
|
|
|
|
2014-11-23 00:52:48 +00:00
|
|
|
// Convert the struct to a more loose structure, and inject the size.
|
2015-04-25 21:53:44 +00:00
|
|
|
output := map[string]interface{}{
|
2015-04-28 21:12:19 +00:00
|
|
|
"progress": s.toNeedSlice(progress),
|
|
|
|
"queued": s.toNeedSlice(queued),
|
|
|
|
"rest": s.toNeedSlice(rest),
|
2015-04-25 21:53:44 +00:00
|
|
|
"total": total,
|
|
|
|
"page": page,
|
|
|
|
"perpage": perpage,
|
2014-11-23 00:52:48 +00:00
|
|
|
}
|
2014-05-19 20:31:28 +00:00
|
|
|
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-11-23 00:52:48 +00:00
|
|
|
json.NewEncoder(w).Encode(output)
|
2014-05-19 20:31:28 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemConnections(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var res = s.model.ConnectionStats()
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 22:58:14 +00:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDeviceStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var res = s.model.DeviceStatistics()
|
2014-08-21 22:46:34 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getFolderStats(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var res = s.model.FolderStatistics()
|
2014-12-07 20:21:12 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBFile(w http.ResponseWriter, r *http.Request) {
|
2015-03-17 17:51:50 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
folder := qs.Get("folder")
|
|
|
|
file := qs.Get("file")
|
2015-04-28 21:12:19 +00:00
|
|
|
gf, _ := s.model.CurrentGlobalFile(folder, file)
|
|
|
|
lf, _ := s.model.CurrentFolderFile(folder, file)
|
2015-03-17 17:51:50 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
av := s.model.Availability(folder, file)
|
2015-03-17 17:51:50 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string]interface{}{
|
2015-04-20 13:37:04 +00:00
|
|
|
"global": jsonFileInfo(gf),
|
|
|
|
"local": jsonFileInfo(lf),
|
2015-03-17 17:51:50 +00:00
|
|
|
"availability": av,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemConfig(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-10-06 07:25:45 +00:00
|
|
|
json.NewEncoder(w).Encode(cfg.Raw())
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemConfig(w http.ResponseWriter, r *http.Request) {
|
2015-06-03 07:47:39 +00:00
|
|
|
s.systemConfigMut.Lock()
|
|
|
|
defer s.systemConfigMut.Unlock()
|
|
|
|
|
|
|
|
var to config.Configuration
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&to)
|
2014-03-02 22:58:14 +00:00
|
|
|
if err != nil {
|
2014-08-17 08:28:36 +00:00
|
|
|
l.Warnln("decoding posted config:", err)
|
2014-08-08 12:09:27 +00:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
2014-12-08 15:36:15 +00:00
|
|
|
}
|
2014-06-07 02:00:46 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
if to.GUI.Password != cfg.GUI().Password {
|
|
|
|
if to.GUI.Password != "" {
|
|
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(to.GUI.Password), 0)
|
2014-06-11 23:05:00 +00:00
|
|
|
if err != nil {
|
2014-12-08 15:36:15 +00:00
|
|
|
l.Warnln("bcrypting password:", err)
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
2014-06-11 23:05:00 +00:00
|
|
|
}
|
2014-12-08 15:36:15 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
to.GUI.Password = string(hash)
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
2014-12-08 15:36:15 +00:00
|
|
|
}
|
2014-06-11 18:04:23 +00:00
|
|
|
|
2015-05-12 07:35:37 +00:00
|
|
|
// Fixup usage reporting settings
|
2014-06-07 02:00:46 +00:00
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
if curAcc := cfg.Options().URAccepted; to.Options.URAccepted > curAcc {
|
2014-12-08 15:36:15 +00:00
|
|
|
// UR was enabled
|
2015-06-03 07:47:39 +00:00
|
|
|
to.Options.URAccepted = usageReportVersion
|
|
|
|
to.Options.URUniqueID = randomString(8)
|
|
|
|
} else if to.Options.URAccepted < curAcc {
|
2014-12-08 15:36:15 +00:00
|
|
|
// UR was disabled
|
2015-06-03 07:47:39 +00:00
|
|
|
to.Options.URAccepted = -1
|
|
|
|
to.Options.URUniqueID = ""
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
2014-12-08 15:36:15 +00:00
|
|
|
|
|
|
|
// Activate and save
|
|
|
|
|
2015-06-03 07:47:39 +00:00
|
|
|
resp := cfg.Replace(to)
|
|
|
|
configInSync = !resp.RequiresRestart
|
2014-12-08 15:36:15 +00:00
|
|
|
cfg.Save()
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemConfigInsync(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 22:58:14 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string]bool{"configInSync": configInSync})
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemRestart(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.flushResponse(`{"ok": "restarting"}`, w)
|
2014-04-03 20:10:51 +00:00
|
|
|
go restart()
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemReset(w http.ResponseWriter, r *http.Request) {
|
2015-04-03 18:06:03 +00:00
|
|
|
var qs = r.URL.Query()
|
|
|
|
folder := qs.Get("folder")
|
2015-06-21 07:35:41 +00:00
|
|
|
|
|
|
|
if len(folder) > 0 {
|
|
|
|
if _, ok := cfg.Folders()[folder]; !ok {
|
|
|
|
http.Error(w, "Invalid folder ID", 500)
|
|
|
|
return
|
2015-06-04 12:35:03 +00:00
|
|
|
}
|
2015-04-03 18:06:03 +00:00
|
|
|
}
|
2015-06-21 07:35:41 +00:00
|
|
|
|
2015-04-03 18:06:03 +00:00
|
|
|
if len(folder) == 0 {
|
2015-06-21 07:35:41 +00:00
|
|
|
// Reset all folders.
|
|
|
|
for folder := range cfg.Folders() {
|
|
|
|
s.model.ResetFolder(folder)
|
|
|
|
}
|
2015-04-28 21:12:19 +00:00
|
|
|
s.flushResponse(`{"ok": "resetting database"}`, w)
|
2015-04-03 18:06:03 +00:00
|
|
|
} else {
|
2015-06-21 07:35:41 +00:00
|
|
|
// Reset a specific folder, assuming it's supposed to exist.
|
|
|
|
s.model.ResetFolder(folder)
|
2015-06-19 06:30:19 +00:00
|
|
|
s.flushResponse(`{"ok": "resetting folder `+folder+`"}`, w)
|
2015-04-03 18:06:03 +00:00
|
|
|
}
|
2015-06-21 07:35:41 +00:00
|
|
|
|
2014-04-03 20:10:51 +00:00
|
|
|
go restart()
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemShutdown(w http.ResponseWriter, r *http.Request) {
|
|
|
|
s.flushResponse(`{"ok": "shutting down"}`, w)
|
2014-05-11 23:16:27 +00:00
|
|
|
go shutdown()
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) flushResponse(resp string, w http.ResponseWriter) {
|
|
|
|
w.Write([]byte(resp + "\n"))
|
2014-05-13 00:15:18 +00:00
|
|
|
f := w.(http.Flusher)
|
|
|
|
f.Flush()
|
|
|
|
}
|
|
|
|
|
2014-04-14 10:02:40 +00:00
|
|
|
var cpuUsagePercent [10]float64 // The last ten seconds
|
2015-04-28 20:32:10 +00:00
|
|
|
var cpuUsageLock = sync.NewRWMutex()
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemStatus(w http.ResponseWriter, r *http.Request) {
|
2014-03-02 22:58:14 +00:00
|
|
|
var m runtime.MemStats
|
|
|
|
runtime.ReadMemStats(&m)
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
tilde, _ := osutil.ExpandTilde("~")
|
2014-03-02 22:58:14 +00:00
|
|
|
res := make(map[string]interface{})
|
2014-06-29 23:42:03 +00:00
|
|
|
res["myID"] = myID.String()
|
2014-03-02 22:58:14 +00:00
|
|
|
res["goroutines"] = runtime.NumGoroutine()
|
|
|
|
res["alloc"] = m.Alloc
|
2014-08-05 20:14:11 +00:00
|
|
|
res["sys"] = m.Sys - m.HeapReleased
|
2014-10-06 07:25:45 +00:00
|
|
|
res["tilde"] = tilde
|
|
|
|
if cfg.Options().GlobalAnnEnabled && discoverer != nil {
|
2014-04-16 15:36:09 +00:00
|
|
|
res["extAnnounceOK"] = discoverer.ExtAnnounceOK()
|
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
cpuUsageLock.RLock()
|
2014-04-14 10:02:40 +00:00
|
|
|
var cpusum float64
|
|
|
|
for _, p := range cpuUsagePercent {
|
|
|
|
cpusum += p
|
|
|
|
}
|
2014-03-02 22:58:14 +00:00
|
|
|
cpuUsageLock.RUnlock()
|
2015-02-25 22:30:24 +00:00
|
|
|
res["cpuPercent"] = cpusum / float64(len(cpuUsagePercent)) / float64(runtime.NumCPU())
|
2014-12-14 23:12:12 +00:00
|
|
|
res["pathSeparator"] = string(filepath.Separator)
|
2015-04-03 18:00:13 +00:00
|
|
|
res["uptime"] = int(time.Since(startTime).Seconds())
|
2014-03-02 22:58:14 +00:00
|
|
|
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 22:58:14 +00:00
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemError(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-03-02 22:58:14 +00:00
|
|
|
guiErrorsMut.Lock()
|
2014-09-18 10:49:59 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string][]guiError{"errors": guiErrors})
|
2014-03-02 22:58:14 +00:00
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemError(w http.ResponseWriter, r *http.Request) {
|
2014-07-05 19:40:29 +00:00
|
|
|
bs, _ := ioutil.ReadAll(r.Body)
|
|
|
|
r.Body.Close()
|
2015-04-28 21:12:19 +00:00
|
|
|
s.showGuiError(0, string(bs))
|
2014-03-02 22:58:14 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemErrorClear(w http.ResponseWriter, r *http.Request) {
|
2014-04-16 14:30:49 +00:00
|
|
|
guiErrorsMut.Lock()
|
2014-05-17 11:54:11 +00:00
|
|
|
guiErrors = []guiError{}
|
2014-04-16 14:30:49 +00:00
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) showGuiError(l logger.LogLevel, err string) {
|
2014-03-02 22:58:14 +00:00
|
|
|
guiErrorsMut.Lock()
|
|
|
|
guiErrors = append(guiErrors, guiError{time.Now(), err})
|
|
|
|
if len(guiErrors) > 5 {
|
|
|
|
guiErrors = guiErrors[len(guiErrors)-5:]
|
|
|
|
}
|
|
|
|
guiErrorsMut.Unlock()
|
|
|
|
}
|
2014-04-19 11:33:51 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemDiscovery(w http.ResponseWriter, r *http.Request) {
|
2014-05-13 00:50:54 +00:00
|
|
|
var qs = r.URL.Query()
|
2014-09-28 11:00:38 +00:00
|
|
|
var device = qs.Get("device")
|
2014-05-13 00:50:54 +00:00
|
|
|
var addr = qs.Get("addr")
|
2014-09-28 11:00:38 +00:00
|
|
|
if len(device) != 0 && len(addr) != 0 && discoverer != nil {
|
|
|
|
discoverer.Hint(device, []string{addr})
|
2014-05-13 00:50:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemDiscovery(w http.ResponseWriter, r *http.Request) {
|
2014-10-15 18:23:52 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-10-28 19:40:04 +00:00
|
|
|
devices := map[string][]discover.CacheEntry{}
|
|
|
|
|
|
|
|
if discoverer != nil {
|
|
|
|
// Device ids can't be marshalled as keys so we need to manually
|
|
|
|
// rebuild this map using strings. Discoverer may be nil if discovery
|
|
|
|
// has not started yet.
|
|
|
|
for device, entries := range discoverer.All() {
|
|
|
|
devices[device.String()] = entries
|
|
|
|
}
|
2014-10-15 18:23:28 +00:00
|
|
|
}
|
2014-10-15 20:52:06 +00:00
|
|
|
|
2014-10-15 18:23:28 +00:00
|
|
|
json.NewEncoder(w).Encode(devices)
|
2014-05-13 01:08:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getReport(w http.ResponseWriter, r *http.Request) {
|
2014-06-22 15:26:31 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2015-04-28 21:12:19 +00:00
|
|
|
json.NewEncoder(w).Encode(reportData(s.model))
|
2014-06-11 18:04:23 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDBIgnores(w http.ResponseWriter, r *http.Request) {
|
2014-09-15 22:12:29 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
ignores, patterns, err := s.model.GetIgnores(qs.Get("folder"))
|
2014-09-15 22:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2014-11-08 21:12:18 +00:00
|
|
|
|
2014-09-15 22:12:29 +00:00
|
|
|
json.NewEncoder(w).Encode(map[string][]string{
|
2014-11-08 21:12:18 +00:00
|
|
|
"ignore": ignores,
|
|
|
|
"patterns": patterns,
|
2014-09-15 22:12:29 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postDBIgnores(w http.ResponseWriter, r *http.Request) {
|
2014-09-15 22:12:29 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
|
|
|
|
var data map[string][]string
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&data)
|
|
|
|
r.Body.Close()
|
2014-09-19 20:02:53 +00:00
|
|
|
|
2014-09-15 22:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
err = s.model.SetIgnores(qs.Get("folder"), data["ignore"])
|
2014-09-15 22:12:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
s.getDBIgnores(w, r)
|
2014-09-15 22:12:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getEvents(w http.ResponseWriter, r *http.Request) {
|
2014-07-13 19:07:24 +00:00
|
|
|
qs := r.URL.Query()
|
2014-07-29 09:06:52 +00:00
|
|
|
sinceStr := qs.Get("since")
|
|
|
|
limitStr := qs.Get("limit")
|
|
|
|
since, _ := strconv.Atoi(sinceStr)
|
|
|
|
limit, _ := strconv.Atoi(limitStr)
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
s.fss.gotEventRequest()
|
2015-03-26 22:26:51 +00:00
|
|
|
|
2014-08-19 22:30:32 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
|
2014-08-19 22:18:28 +00:00
|
|
|
// Flush before blocking, to indicate that we've received the request
|
|
|
|
// and that it should not be retried.
|
|
|
|
f := w.(http.Flusher)
|
|
|
|
f.Flush()
|
|
|
|
|
2015-06-16 07:17:58 +00:00
|
|
|
evs := s.eventSub.Since(since, nil)
|
2014-07-29 09:06:52 +00:00
|
|
|
if 0 < limit && limit < len(evs) {
|
|
|
|
evs = evs[len(evs)-limit:]
|
|
|
|
}
|
2014-07-13 19:07:24 +00:00
|
|
|
|
2014-07-29 09:06:52 +00:00
|
|
|
json.NewEncoder(w).Encode(evs)
|
2014-07-13 19:07:24 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemUpgrade(w http.ResponseWriter, r *http.Request) {
|
2015-01-06 21:40:52 +00:00
|
|
|
if noUpgrade {
|
|
|
|
http.Error(w, upgrade.ErrUpgradeUnsupported.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
2015-04-09 20:44:36 +00:00
|
|
|
rel, err := upgrade.LatestRelease(Version)
|
2014-07-14 08:45:29 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
res := make(map[string]interface{})
|
|
|
|
res["running"] = Version
|
|
|
|
res["latest"] = rel.Tag
|
2015-04-22 12:41:08 +00:00
|
|
|
res["newer"] = upgrade.CompareVersions(rel.Tag, Version) == upgrade.Newer
|
|
|
|
res["majorNewer"] = upgrade.CompareVersions(rel.Tag, Version) == upgrade.MajorNewer
|
2014-07-14 08:45:29 +00:00
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getDeviceID(w http.ResponseWriter, r *http.Request) {
|
2014-07-18 08:00:02 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
idStr := qs.Get("id")
|
2014-09-28 11:00:38 +00:00
|
|
|
id, err := protocol.DeviceIDFromString(idStr)
|
2014-07-18 08:00:02 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
if err == nil {
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"id": id.String(),
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
json.NewEncoder(w).Encode(map[string]string{
|
|
|
|
"error": err.Error(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getLang(w http.ResponseWriter, r *http.Request) {
|
2014-07-26 20:30:29 +00:00
|
|
|
lang := r.Header.Get("Accept-Language")
|
|
|
|
var langs []string
|
|
|
|
for _, l := range strings.Split(lang, ",") {
|
2014-08-14 15:04:17 +00:00
|
|
|
parts := strings.SplitN(l, ";", 2)
|
2014-08-28 11:23:23 +00:00
|
|
|
langs = append(langs, strings.ToLower(strings.TrimSpace(parts[0])))
|
2014-07-26 20:30:29 +00:00
|
|
|
}
|
2014-08-05 07:38:38 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
2014-07-26 20:30:29 +00:00
|
|
|
json.NewEncoder(w).Encode(langs)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postSystemUpgrade(w http.ResponseWriter, r *http.Request) {
|
2015-04-09 20:44:36 +00:00
|
|
|
rel, err := upgrade.LatestRelease(Version)
|
2014-07-14 08:45:29 +00:00
|
|
|
if err != nil {
|
2014-08-17 08:28:36 +00:00
|
|
|
l.Warnln("getting latest release:", err)
|
2014-07-14 08:45:29 +00:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-22 12:41:08 +00:00
|
|
|
if upgrade.CompareVersions(rel.Tag, Version) > upgrade.Equal {
|
2014-12-08 15:36:15 +00:00
|
|
|
err = upgrade.To(rel)
|
2014-07-31 14:01:23 +00:00
|
|
|
if err != nil {
|
2014-08-17 08:28:36 +00:00
|
|
|
l.Warnln("upgrading:", err)
|
2014-07-31 14:01:23 +00:00
|
|
|
http.Error(w, err.Error(), 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
s.flushResponse(`{"ok": "restarting"}`, w)
|
2014-09-13 14:25:39 +00:00
|
|
|
l.Infoln("Upgrading")
|
|
|
|
stop <- exitUpgrading
|
2014-07-31 14:01:23 +00:00
|
|
|
}
|
2014-07-14 08:45:29 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postDBScan(w http.ResponseWriter, r *http.Request) {
|
2014-08-11 18:20:01 +00:00
|
|
|
qs := r.URL.Query()
|
2014-09-28 11:00:38 +00:00
|
|
|
folder := qs.Get("folder")
|
2015-02-11 18:52:59 +00:00
|
|
|
if folder != "" {
|
2015-05-03 12:18:50 +00:00
|
|
|
nextStr := qs.Get("next")
|
|
|
|
next, err := strconv.Atoi(nextStr)
|
|
|
|
if err == nil {
|
|
|
|
s.model.DelayScan(folder, time.Duration(next)*time.Second)
|
|
|
|
}
|
|
|
|
|
2015-03-27 08:51:18 +00:00
|
|
|
subs := qs["sub"]
|
2015-05-03 12:18:50 +00:00
|
|
|
err = s.model.ScanFolderSubs(folder, subs)
|
2015-02-11 18:52:59 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), 500)
|
2015-05-01 12:30:17 +00:00
|
|
|
return
|
2015-02-11 18:52:59 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-04-28 21:12:19 +00:00
|
|
|
errors := s.model.ScanFolders()
|
2015-02-11 18:52:59 +00:00
|
|
|
if len(errors) > 0 {
|
|
|
|
http.Error(w, "Error scanning folders", 500)
|
|
|
|
json.NewEncoder(w).Encode(errors)
|
2015-05-01 12:30:17 +00:00
|
|
|
return
|
2015-02-11 18:52:59 +00:00
|
|
|
}
|
2014-08-11 18:20:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) postDBPrio(w http.ResponseWriter, r *http.Request) {
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 19:23:06 +00:00
|
|
|
qs := r.URL.Query()
|
|
|
|
folder := qs.Get("folder")
|
|
|
|
file := qs.Get("file")
|
2015-04-28 21:12:19 +00:00
|
|
|
s.model.BringToFront(folder, file)
|
|
|
|
s.getDBNeed(w, r)
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 19:23:06 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getQR(w http.ResponseWriter, r *http.Request) {
|
2014-08-04 20:53:37 +00:00
|
|
|
var qs = r.URL.Query()
|
|
|
|
var text = qs.Get("text")
|
2014-07-05 19:40:29 +00:00
|
|
|
code, err := qr.Encode(text, qr.M)
|
2014-05-21 18:06:14 +00:00
|
|
|
if err != nil {
|
|
|
|
http.Error(w, "Invalid", 500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "image/png")
|
|
|
|
w.Write(code.PNG())
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getPeerCompletion(w http.ResponseWriter, r *http.Request) {
|
2014-07-29 11:01:27 +00:00
|
|
|
tot := map[string]float64{}
|
|
|
|
count := map[string]float64{}
|
|
|
|
|
2014-10-06 07:25:45 +00:00
|
|
|
for _, folder := range cfg.Folders() {
|
2014-09-28 11:00:38 +00:00
|
|
|
for _, device := range folder.DeviceIDs() {
|
|
|
|
deviceStr := device.String()
|
2015-04-28 21:12:19 +00:00
|
|
|
if s.model.ConnectedTo(device) {
|
|
|
|
tot[deviceStr] += s.model.Completion(device, folder.ID)
|
2014-07-29 11:01:27 +00:00
|
|
|
} else {
|
2014-09-28 11:00:38 +00:00
|
|
|
tot[deviceStr] = 0
|
2014-07-29 11:01:27 +00:00
|
|
|
}
|
2014-09-28 11:00:38 +00:00
|
|
|
count[deviceStr]++
|
2014-07-29 11:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
comp := map[string]int{}
|
2014-09-28 11:00:38 +00:00
|
|
|
for device := range tot {
|
|
|
|
comp[device] = int(tot[device] / count[device])
|
2014-07-29 11:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
json.NewEncoder(w).Encode(comp)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) getSystemBrowse(w http.ResponseWriter, r *http.Request) {
|
2014-11-16 19:30:49 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
|
|
qs := r.URL.Query()
|
|
|
|
current := qs.Get("current")
|
|
|
|
search, _ := osutil.ExpandTilde(current)
|
|
|
|
pathSeparator := string(os.PathSeparator)
|
|
|
|
if strings.HasSuffix(current, pathSeparator) && !strings.HasSuffix(search, pathSeparator) {
|
|
|
|
search = search + pathSeparator
|
|
|
|
}
|
2015-04-26 14:41:04 +00:00
|
|
|
subdirectories, _ := osutil.Glob(search + "*")
|
2014-11-16 19:30:49 +00:00
|
|
|
ret := make([]string, 0, 10)
|
|
|
|
for _, subdirectory := range subdirectories {
|
|
|
|
info, err := os.Stat(subdirectory)
|
|
|
|
if err == nil && info.IsDir() {
|
2014-11-23 00:52:48 +00:00
|
|
|
ret = append(ret, subdirectory+pathSeparator)
|
2014-11-16 19:30:49 +00:00
|
|
|
if len(ret) > 9 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(ret)
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
type embeddedStatic struct {
|
|
|
|
assetDir string
|
|
|
|
assets map[string][]byte
|
|
|
|
}
|
2014-05-22 14:12:19 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s embeddedStatic) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
file := r.URL.Path
|
2014-05-22 14:12:19 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
if file[0] == '/' {
|
|
|
|
file = file[1:]
|
|
|
|
}
|
2014-05-22 14:12:19 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
if len(file) == 0 {
|
|
|
|
file = "index.html"
|
|
|
|
}
|
2014-05-22 14:12:19 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
if s.assetDir != "" {
|
|
|
|
p := filepath.Join(s.assetDir, filepath.FromSlash(file))
|
|
|
|
_, err := os.Stat(p)
|
|
|
|
if err == nil {
|
|
|
|
http.ServeFile(w, r, p)
|
2014-07-22 18:11:36 +00:00
|
|
|
return
|
|
|
|
}
|
2015-04-28 21:12:19 +00:00
|
|
|
}
|
2014-07-22 18:11:36 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
bs, ok := s.assets[file]
|
|
|
|
if !ok {
|
|
|
|
http.NotFound(w, r)
|
|
|
|
return
|
|
|
|
}
|
2014-07-05 19:40:29 +00:00
|
|
|
|
2015-08-08 11:48:09 +00:00
|
|
|
if r.Header.Get("If-Modified-Since") == auto.AssetsBuildDate {
|
|
|
|
w.WriteHeader(http.StatusNotModified)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
mtype := s.mimeTypeForFile(file)
|
|
|
|
if len(mtype) != 0 {
|
|
|
|
w.Header().Set("Content-Type", mtype)
|
|
|
|
}
|
|
|
|
if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") {
|
|
|
|
w.Header().Set("Content-Encoding", "gzip")
|
|
|
|
} else {
|
|
|
|
// ungzip if browser not send gzip accepted header
|
|
|
|
var gr *gzip.Reader
|
|
|
|
gr, _ = gzip.NewReader(bytes.NewReader(bs))
|
|
|
|
bs, _ = ioutil.ReadAll(gr)
|
|
|
|
gr.Close()
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-Length", fmt.Sprintf("%d", len(bs)))
|
|
|
|
w.Header().Set("Last-Modified", auto.AssetsBuildDate)
|
2015-08-08 11:48:09 +00:00
|
|
|
w.Header().Set("Cache-Control", "public")
|
2015-04-28 21:12:19 +00:00
|
|
|
|
|
|
|
w.Write(bs)
|
2014-05-22 14:12:19 +00:00
|
|
|
}
|
2014-09-04 06:24:42 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s embeddedStatic) mimeTypeForFile(file string) string {
|
2014-09-04 06:24:42 +00:00
|
|
|
// We use a built in table of the common types since the system
|
|
|
|
// TypeByExtension might be unreliable. But if we don't know, we delegate
|
|
|
|
// to the system.
|
|
|
|
ext := filepath.Ext(file)
|
|
|
|
switch ext {
|
|
|
|
case ".htm", ".html":
|
|
|
|
return "text/html"
|
|
|
|
case ".css":
|
|
|
|
return "text/css"
|
|
|
|
case ".js":
|
|
|
|
return "application/javascript"
|
|
|
|
case ".json":
|
|
|
|
return "application/json"
|
|
|
|
case ".png":
|
|
|
|
return "image/png"
|
|
|
|
case ".ttf":
|
|
|
|
return "application/x-font-ttf"
|
2014-09-04 06:47:23 +00:00
|
|
|
case ".woff":
|
|
|
|
return "application/x-font-woff"
|
2015-03-22 11:55:44 +00:00
|
|
|
case ".svg":
|
|
|
|
return "image/svg+xml"
|
2014-09-04 06:24:42 +00:00
|
|
|
default:
|
|
|
|
return mime.TypeByExtension(ext)
|
|
|
|
}
|
|
|
|
}
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 19:23:06 +00:00
|
|
|
|
2015-04-28 21:12:19 +00:00
|
|
|
func (s *apiSvc) toNeedSlice(fs []db.FileInfoTruncated) []jsonDBFileInfo {
|
2015-04-20 13:37:04 +00:00
|
|
|
res := make([]jsonDBFileInfo, len(fs))
|
|
|
|
for i, f := range fs {
|
|
|
|
res[i] = jsonDBFileInfo(f)
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
// Type wrappers for nice JSON serialization
|
|
|
|
|
|
|
|
type jsonFileInfo protocol.FileInfo
|
|
|
|
|
|
|
|
func (f jsonFileInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"name": f.Name,
|
|
|
|
"size": protocol.FileInfo(f).Size(),
|
|
|
|
"flags": fmt.Sprintf("%#o", f.Flags),
|
|
|
|
"modified": time.Unix(f.Modified, 0),
|
|
|
|
"localVersion": f.LocalVersion,
|
|
|
|
"numBlocks": len(f.Blocks),
|
|
|
|
"version": jsonVersionVector(f.Version),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonDBFileInfo db.FileInfoTruncated
|
|
|
|
|
|
|
|
func (f jsonDBFileInfo) MarshalJSON() ([]byte, error) {
|
|
|
|
return json.Marshal(map[string]interface{}{
|
|
|
|
"name": f.Name,
|
|
|
|
"size": db.FileInfoTruncated(f).Size(),
|
|
|
|
"flags": fmt.Sprintf("%#o", f.Flags),
|
|
|
|
"modified": time.Unix(f.Modified, 0),
|
|
|
|
"localVersion": f.LocalVersion,
|
|
|
|
"version": jsonVersionVector(f.Version),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type jsonVersionVector protocol.Vector
|
|
|
|
|
|
|
|
func (v jsonVersionVector) MarshalJSON() ([]byte, error) {
|
|
|
|
res := make([]string, len(v))
|
|
|
|
for i, c := range v {
|
|
|
|
res[i] = fmt.Sprintf("%d:%d", c.ID, c.Value)
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 19:23:06 +00:00
|
|
|
}
|
2015-04-20 13:37:04 +00:00
|
|
|
return json.Marshal(res)
|
Add job queue (fixes #629)
Request to terminate currently ongoing downloads and jump to the bumped file
incoming in 3, 2, 1.
Also, has a slightly strange effect where we pop a job off the queue, but
the copyChannel is still busy and blocks, though it gets moved to the
progress slice in the jobqueue, and looks like it's in progress which it isn't
as it's waiting to be picked up from the copyChan.
As a result, the progress emitter doesn't register on the task, and hence the file
doesn't have a progress bar, but cannot be replaced by a bump.
I guess I can fix progress bar issue by moving the progressEmiter.Register just
before passing the file to the copyChan, but then we are back to the initial
problem of a file with a progress bar, but no progress happening as it's stuck
on write to copyChan
I checked if there is a way to check for channel writeability (before popping)
but got struck by lightning just for bringing the idea up in #go-nuts.
My ideal scenario would be to check if copyChan is writeable, pop job from the
queue and shove it down handleFile. This way jobs would stay in the queue while
they cannot be handled, meaning that the `Bump` could bring your file up higher.
2014-12-01 19:23:06 +00:00
|
|
|
}
|