Refactor DAV handling and remove unused code

This commit is contained in:
abersheeran 2024-01-06 10:10:37 +08:00
parent 4788813c39
commit f81cee4647

View File

@ -49,9 +49,6 @@ async function* listAll(bucket: R2Bucket, prefix: string, isRecursive: boolean =
} while (r2_objects.truncated); } while (r2_objects.truncated);
} }
const DAV_CLASS = '1';
const SUPPORT_METHODS = ['OPTIONS', 'PROPFIND', 'MKCOL', 'GET', 'HEAD', 'PUT', 'COPY', 'MOVE'];
type DavProperties = { type DavProperties = {
creationdate: string | undefined; creationdate: string | undefined;
displayname: string | undefined; displayname: string | undefined;
@ -95,16 +92,6 @@ function make_resource_path(request: Request): string {
return path; return path;
} }
async function handle_options(request: Request, bucket: R2Bucket): Promise<Response> {
return new Response(null, {
status: 204,
headers: {
DAV: DAV_CLASS,
Allow: SUPPORT_METHODS.join(', '),
},
});
}
async function handle_head(request: Request, bucket: R2Bucket): Promise<Response> { async function handle_head(request: Request, bucket: R2Bucket): Promise<Response> {
let response = await handle_get(request, bucket); let response = await handle_get(request, bucket);
return new Response(null, { return new Response(null, {
@ -151,28 +138,28 @@ async function handle_get(request: Request, bucket: R2Bucket): Promise<Response>
...(object.httpMetadata?.contentDisposition ...(object.httpMetadata?.contentDisposition
? { ? {
'Content-Disposition': object.httpMetadata.contentDisposition, 'Content-Disposition': object.httpMetadata.contentDisposition,
} }
: {}), : {}),
...(object.httpMetadata?.contentEncoding ...(object.httpMetadata?.contentEncoding
? { ? {
'Content-Encoding': object.httpMetadata.contentEncoding, 'Content-Encoding': object.httpMetadata.contentEncoding,
} }
: {}), : {}),
...(object.httpMetadata?.contentLanguage ...(object.httpMetadata?.contentLanguage
? { ? {
'Content-Language': object.httpMetadata.contentLanguage, 'Content-Language': object.httpMetadata.contentLanguage,
} }
: {}), : {}),
...(object.httpMetadata?.cacheControl ...(object.httpMetadata?.cacheControl
? { ? {
'Cache-Control': object.httpMetadata.cacheControl, 'Cache-Control': object.httpMetadata.cacheControl,
} }
: {}), : {}),
...(object.httpMetadata?.cacheExpiry ...(object.httpMetadata?.cacheExpiry
? { ? {
'Cache-Expiry': object.httpMetadata.cacheExpiry.toISOString(), 'Cache-Expiry': object.httpMetadata.cacheExpiry.toISOString(),
} }
: {}), : {}),
}, },
}); });
@ -305,9 +292,9 @@ function generate_propfind_response(object: R2Object | null): string {
<propstat> <propstat>
<prop> <prop>
${Object.entries(fromR2Object(object)) ${Object.entries(fromR2Object(object))
.filter(([_, value]) => value !== undefined) .filter(([_, value]) => value !== undefined)
.map(([key, value]) => `<${key}>${value}</${key}>`) .map(([key, value]) => `<${key}>${value}</${key}>`)
.join('\n ')} .join('\n ')}
</prop> </prop>
<status>HTTP/1.1 200 OK</status> <status>HTTP/1.1 200 OK</status>
</propstat> </propstat>
@ -570,10 +557,19 @@ async function handle_move(request: Request, bucket: R2Bucket): Promise<Response
} }
} }
const DAV_CLASS = '1';
const SUPPORT_METHODS = ['OPTIONS', 'PROPFIND', 'MKCOL', 'GET', 'HEAD', 'PUT', 'COPY', 'MOVE'];
async function dispatch_handler(request: Request, bucket: R2Bucket): Promise<Response> { async function dispatch_handler(request: Request, bucket: R2Bucket): Promise<Response> {
switch (request.method) { switch (request.method) {
case 'OPTIONS': { case 'OPTIONS': {
return await handle_options(request, bucket); return new Response(null, {
status: 204,
headers: {
Allow: SUPPORT_METHODS.join(', '),
DAV: DAV_CLASS,
},
});
} }
case 'HEAD': { case 'HEAD': {
return await handle_head(request, bucket); return await handle_head(request, bucket);