返回 DeepSeek-Reasonix
attachment_read.go
根目录 / internal / session / attachment_read.go
1 package session
2
3 import (
4 "context"
5 "database/sql"
6 "errors"
7 "path/filepath"
8 "strings"
9
10 "reasonix/internal/attachment"
11 "reasonix/internal/sessioncontent"
12 )
13
14 const sessionAttachmentReadLimit = 1 << 20
15
16 // ReadSessionAttachment returns one bounded range of an original attachment.
17 // The caller supplies only the digest; size and integrity fields are looked up
18 // from this session's content graph and are never taken from the client.
19 func (q *Query) ReadSessionAttachment(ctx context.Context, ref SessionRef, digest string, offset, length int64) ([]byte, int64, error) {
20 if q == nil {
21 return nil, 0, errors.New("session: nil query")
22 }
23 if err := ctx.Err(); err != nil {
24 return nil, 0, err
25 }
26 if err := ref.validate(q.hostID); err != nil {
27 return nil, 0, err
28 }
29 digest, ok := canonicalContentDigest(digest)
30 if !ok {
31 return nil, 0, errors.New("session: attachment digest is invalid")
32 }
33 filesystem, ok := q.persistence.(*FilesystemPersistence)
34 if !ok {
35 return nil, 0, errors.New("session: content reads require filesystem persistence")
36 }
37 authorized, err := q.lookupAuthorizedAttachment(ctx, filesystem, ref, digest)
38 if err != nil {
39 return nil, 0, err
40 }
41 if offset < 0 || length < 0 || length > sessionAttachmentReadLimit || offset > authorized.Bytes {
42 return nil, 0, errors.New("session: invalid or oversized attachment range")
43 }
44 if offset == authorized.Bytes {
45 return []byte{}, authorized.Bytes, nil
46 }
47 if length == 0 || length > authorized.Bytes-offset {
48 length = min(int64(sessionAttachmentReadLimit), authorized.Bytes-offset)
49 }
50 data, err := contentStoreForSessionDir(filepath.Join(filesystem.Root, ref.SessionID)).ReadRange(ctx, authorized, offset, length)
51 if err != nil {
52 return nil, 0, err
53 }
54 return data, authorized.Bytes, nil
55 }
56
57 func (q *Query) lookupAuthorizedAttachment(ctx context.Context, filesystem *FilesystemPersistence, ref SessionRef, digest string) (sessioncontent.Ref, error) {
58 if found, ok, err := q.lookupHistoryAttachment(ctx, filesystem, ref, digest); err != nil {
59 return sessioncontent.Ref{}, err
60 } else if ok {
61 return found, nil
62 }
63 if found, ok := q.lookupLiveAttachment(ctx, ref, digest); ok {
64 return found, nil
65 }
66 sessionDir := filepath.Join(filesystem.Root, ref.SessionID)
67 for _, extra := range collectInboxContentRefs(sessionDir) {
68 if extra.Digest == digest {
69 return withIntegrityBlock(extra), nil
70 }
71 }
72 return sessioncontent.Ref{}, errors.New("session: attachment is not authorized for this session")
73 }
74
75 func (q *Query) lookupHistoryAttachment(ctx context.Context, filesystem *FilesystemPersistence, ref SessionRef, digest string) (sessioncontent.Ref, bool, error) {
76 path := historyIndexPath(filesystem.Root, ref.SessionID)
77 if err := ensureHistoryIndex(ctx, filesystem, ref.SessionID, path); err != nil {
78 return sessioncontent.Ref{}, false, err
79 }
80 handle, err := openHistoryProjection(ctx, path)
81 if err != nil {
82 return sessioncontent.Ref{}, false, err
83 }
84 defer handle.DB.Close()
85 var bytes int64
86 var indexDigest string
87 err = handle.DB.QueryRowContext(ctx, `SELECT bytes, index_digest FROM content_refs WHERE digest=? LIMIT 1`, digest).Scan(&bytes, &indexDigest)
88 if errors.Is(err, sql.ErrNoRows) {
89 return sessioncontent.Ref{}, false, nil
90 }
91 if err != nil {
92 return sessioncontent.Ref{}, false, err
93 }
94 found := sessioncontent.Ref{Digest: digest, Bytes: bytes, IndexDigest: indexDigest}
95 return withIntegrityBlock(found), true, nil
96 }
97
98 func (q *Query) lookupLiveAttachment(ctx context.Context, ref SessionRef, digest string) (sessioncontent.Ref, bool) {
99 snapshot, err := q.Snapshot(ctx, ref)
100 if err != nil {
101 return sessioncontent.Ref{}, false
102 }
103 for _, message := range snapshot.Projection.Messages {
104 for _, extra := range attachment.CollectContentRefs(message.ImageInputs) {
105 if extra.Digest == digest {
106 return withIntegrityBlock(extra), true
107 }
108 }
109 }
110 return sessioncontent.Ref{}, false
111 }
112
113 func canonicalContentDigest(digest string) (string, bool) {
114 digest = strings.ToLower(strings.TrimSpace(digest))
115 if len(digest) != 64 {
116 return "", false
117 }
118 for _, c := range digest {
119 if (c < '0' || c > '9') && (c < 'a' || c > 'f') {
120 return "", false
121 }
122 }
123 return digest, true
124 }
125
126 func withIntegrityBlock(ref sessioncontent.Ref) sessioncontent.Ref {
127 if ref.IndexDigest != "" && ref.IntegrityBlock == 0 {
128 ref.IntegrityBlock = sessioncontent.IntegrityBlockBytes
129 }
130 return ref
131 }
132
132 lines GO