Fix server file browse flow
This commit is contained in:
@@ -14,6 +14,7 @@ import (
|
||||
)
|
||||
|
||||
const artifactDownloadStorageBehavior = "platform-durable-artifact-store"
|
||||
const artifactPayloadCacheLimit = 4 * 1024 * 1024
|
||||
|
||||
func (svc *CoreService) GetArtifactForSession(sessionID string, artifactID string) (domain.Artifact, error) {
|
||||
artifact, err := svc.store.Artifacts().Get(strings.TrimSpace(artifactID))
|
||||
@@ -73,29 +74,22 @@ func (svc *CoreService) ReadArtifactContentForSession(sessionID string, request
|
||||
if artifact.State != domain.ArtifactStateAvailable {
|
||||
return domain.ArtifactContent{}, validationError("artifact must be available before download")
|
||||
}
|
||||
payload, err := svc.artifactPayload(artifact.ID)
|
||||
if err != nil {
|
||||
return domain.ArtifactContent{}, err
|
||||
}
|
||||
if int64(len(payload)) != artifact.SizeBytes {
|
||||
return domain.ArtifactContent{}, validationError("artifact content size does not match metadata")
|
||||
}
|
||||
if checksum := validator.BytesChecksum(payload); checksum != artifact.Checksum {
|
||||
return domain.ArtifactContent{}, validationError("artifact content checksum does not match metadata")
|
||||
}
|
||||
if request.Offset >= artifact.SizeBytes {
|
||||
return domain.ArtifactContent{}, validationError("offset must be inside artifact content")
|
||||
}
|
||||
limit := request.Limit
|
||||
if limit == 0 {
|
||||
limit = validator.MaxArtifactDownloadBytes
|
||||
}
|
||||
if request.Offset > artifact.SizeBytes {
|
||||
return domain.ArtifactContent{}, validationError("artifact range exceeds metadata")
|
||||
}
|
||||
remaining := artifact.SizeBytes - request.Offset
|
||||
if int64(limit) > remaining {
|
||||
limit = int(remaining)
|
||||
}
|
||||
end := int(request.Offset) + limit
|
||||
part := domain.CopyBytes(payload[int(request.Offset):end])
|
||||
payload, err := svc.artifactStore.ReadPayloadRange(artifact.ID, request.Offset, limit)
|
||||
if err != nil {
|
||||
return domain.ArtifactContent{}, err
|
||||
}
|
||||
part := domain.CopyBytes(payload)
|
||||
filename, contentType := svc.artifactDownloadPresentation(artifact)
|
||||
content := domain.ArtifactContent{
|
||||
ArtifactID: artifact.ID,
|
||||
@@ -162,7 +156,7 @@ func (svc *CoreService) artifactPayload(artifactID string) ([]byte, error) {
|
||||
return domain.CopyBytes(payload), nil
|
||||
}
|
||||
if payload, err := svc.artifactStore.GetPayload(artifactID); err == nil {
|
||||
svc.artifactPayloads[artifactID] = domain.CopyBytes(payload)
|
||||
svc.cacheArtifactPayload(artifactID, payload)
|
||||
return payload, nil
|
||||
} else if !errors.Is(err, repo.ErrNotFound) {
|
||||
return nil, err
|
||||
@@ -193,10 +187,18 @@ func (svc *CoreService) artifactPayload(artifactID string) ([]byte, error) {
|
||||
if err := svc.artifactStore.PutPayload(artifactID, payload); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
svc.artifactPayloads[artifactID] = domain.CopyBytes(payload)
|
||||
svc.cacheArtifactPayload(artifactID, payload)
|
||||
return payload, nil
|
||||
}
|
||||
|
||||
func (svc *CoreService) cacheArtifactPayload(artifactID string, payload []byte) {
|
||||
if len(payload) > artifactPayloadCacheLimit {
|
||||
delete(svc.artifactPayloads, artifactID)
|
||||
return
|
||||
}
|
||||
svc.artifactPayloads[artifactID] = domain.CopyBytes(payload)
|
||||
}
|
||||
|
||||
func artifactDownloadFilename(artifactID string) string {
|
||||
name := strings.TrimSpace(artifactID)
|
||||
if name == "" || strings.Contains(name, "/") || strings.Contains(name, `\`) || strings.Contains(name, "://") {
|
||||
|
||||
Reference in New Issue
Block a user