Skip to content

Commit bacc507

Browse files
JiataiWangclaudePeter Steinberger
authored
fix(msteams): rename SharePoint uploads on name conflict (#113560)
Bot-generated files reuse names (openclaw runtime emits image-1.png every generation), so Graph's default conflictBehavior=replace overwrote the prior same-named driveItem in place. Because Teams caches file cards by driveItem URL, channels kept showing the stale file and earlier content was lost from OpenClawShared. Force @microsoft.graph.conflictBehavior=rename on the simple upload so SharePoint mints a uniquely-named driveItem (fresh name/eTag/URL) on collision. The existing getDriveItemProperties re-read by item id already surfaces the new name/URL to the Teams file card, so no downstream change is needed. Closes #112368 Co-authored-by: Claude Opus 4.8 <[email protected]> Co-authored-by: Peter Steinberger <[email protected]>
1 parent de6bd7d commit bacc507

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

‎extensions/msteams/src/graph-upload.test.ts‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ describe("graph upload helpers", () => {
156156

157157
expectGraphUploadFetch(
158158
fetchFn,
159-
"https://graph.microsoft.com/v1.0/sites/site-123/drive/root:/OpenClawShared/b.txt:/content",
159+
"https://graph.microsoft.com/v1.0/sites/site-123/drive/root:/OpenClawShared/b.txt:/content[email protected]=rename",
160160
);
161161
expect(result).toEqual({
162162
id: "item-2",
@@ -165,6 +165,37 @@ describe("graph upload helpers", () => {
165165
});
166166
});
167167

168+
it("uploads with conflictBehavior=rename and surfaces the name SharePoint assigns", async () => {
169+
// Regression: openclaw-runtime image assets reuse names (image-1.png). Graph's default
170+
// replace overwrote the prior file and Teams (caching cards by driveItem URL) showed the
171+
// stale image; rename mints a distinct item, so callers use the returned name, not the request.
172+
const fetchFn = vi.fn(
173+
async () =>
174+
new Response(
175+
JSON.stringify({
176+
id: "item-9",
177+
webUrl: "https://example.com/9",
178+
name: "image-1 1.png",
179+
}),
180+
{ status: 200, headers: { "content-type": "application/json" } },
181+
),
182+
);
183+
184+
const result = await uploadToSharePoint({
185+
buffer: Buffer.from("img"),
186+
filename: "image-1.png",
187+
siteId: "site-123",
188+
tokenProvider,
189+
fetchFn: withFetchPreconnect(fetchFn),
190+
});
191+
192+
expectGraphUploadFetch(
193+
fetchFn,
194+
"https://graph.microsoft.com/v1.0/sites/site-123/drive/root:/OpenClawShared/image-1.png:/[email protected]=rename",
195+
);
196+
expect(result.name).toBe("image-1 1.png");
197+
});
198+
168199
it("rejects upload responses missing required fields", async () => {
169200
const fetchFn = vi.fn(
170201
async () =>

‎extensions/msteams/src/graph-upload.ts‎

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -75,26 +75,27 @@ async function uploadToSharePoint(params: {
7575

7676
// Use "OpenClawShared" folder to organize bot-uploaded files
7777
const uploadPath = `/OpenClawShared/${encodeURIComponent(params.filename)}`;
78+
// Graph's default conflictBehavior=replace overwrites a same-named file in place. Bot assets
79+
// reuse names (image-1.png each generation) and Teams caches file cards by driveItem URL, so
80+
// replace clobbers history and shows stale images; "rename" mints a unique driveItem instead.
81+
const uploadUrl = `${GRAPH_ROOT}/sites/${params.siteId}/drive/root:${uploadPath}:/[email protected]=rename`;
7882
const timeoutMs = resolveMSTeamsSharePointUploadTimeoutMs(params.buffer.length);
7983

8084
const data = await withMSTeamsAbortableRequestTimeout({
8185
label: SHAREPOINT_UPLOAD_TIMEOUT_LABEL,
8286
timeoutMs,
8387
work: async (signal) => {
8488
const token = await getGraphAccessToken(params.tokenProvider);
85-
const res = await fetchFn(
86-
`${GRAPH_ROOT}/sites/${params.siteId}/drive/root:${uploadPath}:/content`,
87-
{
88-
method: "PUT",
89-
headers: {
90-
"User-Agent": buildUserAgent(),
91-
Authorization: `Bearer ${token}`,
92-
"Content-Type": params.contentType ?? "application/octet-stream",
93-
},
94-
body: new Uint8Array(params.buffer),
95-
signal,
89+
const res = await fetchFn(uploadUrl, {
90+
method: "PUT",
91+
headers: {
92+
"User-Agent": buildUserAgent(),
93+
Authorization: `Bearer ${token}`,
94+
"Content-Type": params.contentType ?? "application/octet-stream",
9695
},
97-
);
96+
body: new Uint8Array(params.buffer),
97+
signal,
98+
});
9899

99100
if (!res.ok) {
100101
throw await createMSTeamsHttpError(res, "SharePoint upload failed");

0 commit comments

Comments
 (0)