diff --git a/client/lib/lib.go b/client/lib/lib.go index 69df0e4..fc9712c 100644 --- a/client/lib/lib.go +++ b/client/lib/lib.go @@ -999,13 +999,17 @@ func ReliableDbCreate(db *gorm.DB, entry interface{}) error { return fmt.Errorf("failed to create DB entry even with %d retries: %v", i, err) } -func EncryptAndMarshal(config hctx.ClientConfig, entry *data.HistoryEntry) ([]byte, error) { - encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry) - if err != nil { - return nil, fmt.Errorf("failed to encrypt history entry") +func EncryptAndMarshal(config hctx.ClientConfig, entries []*data.HistoryEntry) ([]byte, error) { + var encEntries []shared.EncHistoryEntry + for _, entry := range entries { + encEntry, err := data.EncryptHistoryEntry(config.UserSecret, *entry) + if err != nil { + return nil, fmt.Errorf("failed to encrypt history entry") + } + encEntry.DeviceId = config.DeviceId + encEntries = append(encEntries, encEntry) } - encEntry.DeviceId = config.DeviceId - jsonValue, err := json.Marshal([]shared.EncHistoryEntry{encEntry}) + jsonValue, err := json.Marshal(encEntries) if err != nil { return jsonValue, fmt.Errorf("failed to marshal encrypted history entry: %v", err) } @@ -1074,3 +1078,20 @@ func deleteOnRemoteInstances(ctx *context.Context, historyEntries []*data.Histor } return nil } + +func Reupload(ctx *context.Context) error { + config := hctx.GetConf(ctx) + entries, err := data.Search(hctx.GetDb(ctx), "", 0) + if err != nil { + return fmt.Errorf("failed to reupload due to failed search: %v", err) + } + jsonValue, err := EncryptAndMarshal(config, entries) + if err != nil { + return fmt.Errorf("failed to reupload due to failed encryption: %v", err) + } + _, err = ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) + if err != nil { + return fmt.Errorf("failed to reupload due to failed POST: %v", err) + } + return nil +} diff --git a/hishtory.go b/hishtory.go index 64c62eb..70138b1 100644 --- a/hishtory.go +++ b/hishtory.go @@ -109,16 +109,7 @@ func main() { } case "reupload": // Purposefully undocumented since this command is generally not necessary to run - ctx := hctx.MakeContext() - config := hctx.GetConf(ctx) - entries, err := data.Search(hctx.GetDb(ctx), "", 0) - lib.CheckFatalError(err) - for _, entry := range entries { - jsonValue, err := lib.EncryptAndMarshal(config, entry) - lib.CheckFatalError(err) - _, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) - lib.CheckFatalError(err) - } + lib.CheckFatalError(lib.Reupload(hctx.MakeContext())) case "-h": fallthrough case "help": @@ -273,7 +264,7 @@ func maybeUploadSkippedHistoryEntries(ctx *context.Context) error { } hctx.GetLogger().Printf("Uploading %d history entries that previously failed to upload (query=%#v)\n", len(entries), query) for _, entry := range entries { - jsonValue, err := lib.EncryptAndMarshal(config, entry) + jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry}) if err != nil { return err } @@ -313,7 +304,7 @@ func saveHistoryEntry(ctx *context.Context) { lib.CheckFatalError(err) // Persist it remotely - jsonValue, err := lib.EncryptAndMarshal(config, entry) + jsonValue, err := lib.EncryptAndMarshal(config, []*data.HistoryEntry{entry}) lib.CheckFatalError(err) _, err = lib.ApiPost("/api/v1/submit?source_device_id="+config.DeviceId, "application/json", jsonValue) if err != nil {