-
Notifications
You must be signed in to change notification settings - Fork 141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a trigger for saving OO documents #3751
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,111 @@ | ||
package office | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"io" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/cozy/cozy-stack/model/instance" | ||
"github.com/cozy/cozy-stack/model/job" | ||
jwt "github.com/golang-jwt/jwt/v4" | ||
"github.com/labstack/echo/v4" | ||
) | ||
|
||
// SendSaveMessage is used by the trigger for asking OO to save the document in | ||
// the Cozy. | ||
type SendSaveMessage struct { | ||
Key string `json:"key"` | ||
} | ||
|
||
func setupTrigger(inst *instance.Instance, key string) error { | ||
sched := job.System() | ||
infos := job.TriggerInfos{ | ||
Type: "@every", | ||
WorkerType: "office-save", | ||
Arguments: "10m", | ||
} | ||
msg := &SendSaveMessage{Key: key} | ||
t, err := job.NewTrigger(inst, infos, msg) | ||
if err != nil { | ||
return err | ||
} | ||
return sched.AddTrigger(t) | ||
} | ||
|
||
type commandRequest struct { | ||
Command string `json:"c"` | ||
Key string `json:"key"` | ||
Userdata string `json:"userdata"` | ||
Token string `json:"token,omitempty"` | ||
} | ||
|
||
// Valid is required by the jwt.Claims interface | ||
func (c *commandRequest) Valid() error { return nil } | ||
|
||
type commandResponse struct { | ||
Key string `json:"key"` | ||
Error int `json:"error"` | ||
} | ||
|
||
func SendSave(inst *instance.Instance, msg SendSaveMessage) error { | ||
if _, err := GetStore().GetDoc(inst, msg.Key); err != nil { | ||
// By returning the ErrBadTrigger code, the stack will know that it | ||
// must delete the trigger. | ||
return job.ErrBadTrigger{Err: err} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this how the trigger will be removed once the user is done editing? |
||
} | ||
cfg := getConfig(inst.ContextName) | ||
|
||
cmd := &commandRequest{ | ||
Command: "forcesave", | ||
Key: msg.Key, | ||
Userdata: "stack", | ||
} | ||
if cfg.InboxSecret != "" { | ||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, cmd) | ||
signed, err := token.SignedString([]byte(cfg.InboxSecret)) | ||
if err != nil { | ||
return err | ||
} | ||
cmd.Token = signed | ||
} | ||
body, err := json.Marshal(cmd) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
u, err := url.Parse(cfg.OnlyOfficeURL) | ||
if err != nil { | ||
return err | ||
} | ||
u.Path = strings.TrimSuffix(u.Path, "/") | ||
u.Path += "/coauthoring/CommandService.ashx" | ||
commandURL := u.String() | ||
|
||
res, err := docserverClient.Post(commandURL, echo.MIMEApplicationJSON, bytes.NewReader(body)) | ||
if err != nil { | ||
return err | ||
} | ||
defer func() { | ||
// Flush the body to allow reusing the connection with Keep-Alive | ||
_, _ = io.Copy(io.Discard, res.Body) | ||
_ = res.Body.Close() | ||
}() | ||
var cmdRes commandResponse | ||
if err := json.NewDecoder(res.Body).Decode(&cmdRes); err != nil { | ||
return err | ||
} | ||
// 0 means OK to save, and 4 means that the doc has not changed | ||
if cmdRes.Error != 0 && cmdRes.Error != 4 { | ||
inst.Logger().WithNamespace("office"). | ||
Warnf("error for forcesave %s: %d", msg.Key, cmdRes.Error) | ||
} | ||
// 1 and 3 means that something unexpected happens, 1 for OO side, 3 for | ||
// the stack side: in both cases, we delete the invalid trigger | ||
if cmdRes.Error == 1 || cmdRes.Error == 3 { | ||
return job.ErrBadTrigger{Err: errors.New("unexpected state")} | ||
} | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package office | ||
|
||
import ( | ||
"runtime" | ||
"time" | ||
|
||
"github.com/cozy/cozy-stack/model/job" | ||
"github.com/cozy/cozy-stack/model/office" | ||
) | ||
|
||
func init() { | ||
job.AddWorker(&job.WorkerConfig{ | ||
WorkerType: "office-save", | ||
Concurrency: runtime.NumCPU(), | ||
MaxExecCount: 2, | ||
Reserved: true, | ||
Timeout: 30 * time.Second, | ||
WorkerFunc: WorkerSave, | ||
}) | ||
} | ||
|
||
// WorkerSave is used for asking OnlyOffice to save a document in the Cozy. | ||
func WorkerSave(ctx *job.WorkerContext) error { | ||
var msg office.SendSaveMessage | ||
if err := ctx.UnmarshalMessage(&msg); err != nil { | ||
return err | ||
} | ||
return office.SendSave(ctx.Instance, msg) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we check if a trigger already exists to avoid creating multiple triggers in case a document is opened in multiple tabs?