file browser improvements and hardening.
This commit is contained in:
@@ -33,15 +33,16 @@ import (
|
||||
var ErrReconnect = errors.New("reconnect requested")
|
||||
|
||||
var (
|
||||
activeCommands = make(map[string]context.CancelFunc)
|
||||
activeCommandsMu sync.Mutex
|
||||
voiceSessionMu sync.Mutex
|
||||
voiceSession *voiceRuntime
|
||||
desktopAudioMu sync.Mutex
|
||||
desktopAudioSession *voiceRuntime
|
||||
backstageInputOnce sync.Once
|
||||
backstageInputQueue chan backstageInputEvent
|
||||
backstageInputDropped atomic.Uint64
|
||||
activeCommands = make(map[string]context.CancelFunc)
|
||||
activeCommandsMu sync.Mutex
|
||||
voiceSessionMu sync.Mutex
|
||||
voiceSession *voiceRuntime
|
||||
desktopAudioMu sync.Mutex
|
||||
desktopAudioSession *voiceRuntime
|
||||
backstageInputOnce sync.Once
|
||||
backstageInputQueue chan backstageInputEvent
|
||||
backstageInputDropped atomic.Uint64
|
||||
fileHashSlots = make(chan struct{}, 2)
|
||||
)
|
||||
|
||||
type backstageInputKind int
|
||||
@@ -2697,8 +2698,17 @@ func HandleCommand(ctx context.Context, env *runtime.Env, envelope map[string]in
|
||||
payload, _ := envelope["payload"].(map[string]interface{})
|
||||
path, _ := payload["path"].(string)
|
||||
algorithm, _ := payload["algorithm"].(string)
|
||||
hashCtx, cancel := context.WithCancel(ctx)
|
||||
registerCancellableCommand(cmdID, cancel)
|
||||
goSafe("file_hash", env.Cancel, func() {
|
||||
if err := HandleFileHash(ctx, env, cmdID, path, algorithm); err != nil && err != context.Canceled {
|
||||
defer unregisterCommand(cmdID)
|
||||
select {
|
||||
case fileHashSlots <- struct{}{}:
|
||||
defer func() { <-fileHashSlots }()
|
||||
case <-hashCtx.Done():
|
||||
return
|
||||
}
|
||||
if err := HandleFileHash(hashCtx, env, cmdID, path, algorithm); err != nil && err != context.Canceled {
|
||||
log.Printf("file_hash error: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
@@ -359,16 +359,30 @@ func HandleFileHash(ctx context.Context, env *agentRuntime.Env, cmdID string, pa
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return wire.WriteMsg(ctx, env.Conn, wire.FileHashResult{
|
||||
Type: "file_hash_result",
|
||||
CommandID: cmdID,
|
||||
Path: path,
|
||||
Algorithm: algorithm,
|
||||
Error: err.Error(),
|
||||
})
|
||||
buf := make([]byte, 256*1024)
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
n, readErr := f.Read(buf)
|
||||
if n > 0 {
|
||||
if _, err := h.Write(buf[:n]); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if readErr == io.EOF {
|
||||
break
|
||||
}
|
||||
if readErr != nil {
|
||||
return wire.WriteMsg(ctx, env.Conn, wire.FileHashResult{
|
||||
Type: "file_hash_result", CommandID: cmdID, Path: path,
|
||||
Algorithm: algorithm, Error: readErr.Error(),
|
||||
})
|
||||
}
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return wire.WriteMsg(ctx, env.Conn, wire.FileHashResult{
|
||||
Type: "file_hash_result",
|
||||
CommandID: cmdID,
|
||||
|
||||
Reference in New Issue
Block a user