-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
98 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,90 +1,66 @@ | ||
import { WebDAVProps } from '../types'; | ||
|
||
export async function* listAll(bucket: R2Bucket, prefix: string, isRecursive = false) { | ||
export async function* listAll(bucket: R2Bucket, prefix: string) { | ||
let cursor: string | undefined = undefined; | ||
do { | ||
const r2_objects = await bucket.list({ | ||
prefix, | ||
delimiter: isRecursive ? undefined : "/", | ||
delimiter: "/", | ||
cursor, | ||
include: ["httpMetadata", "customMetadata"] | ||
}); | ||
for (const object of r2_objects.objects) { | ||
yield object; | ||
} | ||
for (const prefix of r2_objects.delimitedPrefixes) { | ||
yield { key: prefix, customMetadata: { resourcetype: 'collection' } } as R2Object; | ||
yield { key: prefix, customMetadata: { resourcetype: "collection" } } as R2Object; | ||
} | ||
cursor = r2_objects.truncd ? r2_objects.cursor : undefined; | ||
cursor = r2_objects.truncated ? r2_objects.cursor : undefined; | ||
} while (cursor); | ||
} | ||
|
||
export function fromR2Object(object: R2Object, bucketName: string): WebDAVProps { | ||
const isCollection = object.key.endsWith('/') || object.customMetadata?.resourcetype === 'collection'; | ||
const displayName = object.key.split('/').pop() || object.key; | ||
export function fromR2Object(object: R2Object, basePath: string): WebDAVProps { | ||
const isCollection = object.customMetadata?.resourcetype === "collection"; | ||
const relativePath = object.key.slice(basePath.length); | ||
return { | ||
href: `/${bucketName}/${object.key}${isCollection ? '/' : ''}`, | ||
creationdate: object.uploaded?.toISOString() || new Date().toISOString(), | ||
displayname: displayName, | ||
getcontentlanguage: object.httpMetadata?.contentLanguage || '', | ||
getcontentlength: isCollection ? '0' : object.size?.toString() || '0', | ||
getcontenttype: isCollection ? 'httpd/unix-directory' : (object.httpMetadata?.contentType || 'application/octet-stream'), | ||
getetag: object.etag || `"${new Date().getTime().toString(16)}"`, | ||
getlastmodified: object.uploaded?.toUTCString() || new Date().toUTCString(), | ||
resourcetype: isCollection ? 'collection' : '' | ||
creationdate: object.uploaded?.toISOString() ?? new Date().toISOString(), | ||
displayname: relativePath || "/", | ||
getcontentlength: isCollection ? "0" : object.size.toString(), | ||
getcontenttype: isCollection ? "httpd/unix-directory" : (object.httpMetadata?.contentType ?? "application/octet-stream"), | ||
getetag: object.etag ?? "", | ||
getlastmodified: object.uploaded?.toUTCString() ?? new Date().toUTCString(), | ||
resourcetype: isCollection ? "<D:collection/>" : "", | ||
iscollection: isCollection | ||
}; | ||
} | ||
|
||
export function make_resource_path(request: Request): string { | ||
let path = new URL(request.url).pathname.slice(1); | ||
return path.endsWith("/") ? path.slice(0, -1) : path; | ||
return new URL(request.url).pathname.slice(1); | ||
} | ||
|
||
export function generatePropfindResponse(bucketName: string, basePath: string, props: WebDAVProps[]): string { | ||
const xml = `<?xml version="1.0" encoding="utf-8"?> | ||
<D:multistatus xmlns:D="DAV:"> | ||
${props.map(prop => generatePropResponse(prop)).join('\n')} | ||
${props.map(prop => generatePropResponse(bucketName, basePath, prop)).join('\n')} | ||
</D:multistatus>`; | ||
return xml; | ||
} | ||
|
||
function generatePropResponse(prop: WebDAVProps): string { | ||
function generatePropResponse(bucketName: string, basePath: string, prop: WebDAVProps): string { | ||
const resourcePath = `/${bucketName}/${basePath}${prop.displayname}${prop.iscollection ? '/' : ''}`; | ||
return ` <D:response> | ||
<D:href>${escapeXml(prop.href)}</D:href> | ||
<D:href>${resourcePath}</D:href> | ||
<D:propstat> | ||
<D:prop> | ||
<D:creationdate>${prop.creationdate}</D:creationdate> | ||
<D:displayname>${escapeXml(prop.displayname)}</D:displayname> | ||
<D:getcontentlanguage>${escapeXml(prop.getcontentlanguage)}</D:getcontentlanguage> | ||
<D:displayname>${prop.displayname}</> | ||
<D:getcontentlength>${prop.getcontentlength}</D:getcontentlength> | ||
<D:getcontenttype>${escapeXml(prop.getcontenttype)}</D:getcontenttype> | ||
<D:getetag>${escapeXml(prop.getet:getetag> | ||
<D:getcontenttype>${prop.getcontenttype}</D:getcontenttype> | ||
<D:getetag>${prop.getetag}</D:getetag> | ||
<D:getlastmodified>${prop.getlastmodified}</D:getlastmodified> | ||
<D:resourcetype>${prop.resourcetype === 'collection' ? '<D:collection/>' : ''}</D:resourcetype> | ||
<D:supportedlock> | ||
<D:lockentry> | ||
<D:lockscope><D:exclusive/></D:lockscope> | ||
<D:locktype><D:write/></D:locktype> | ||
</D:lockentry> | ||
<D:lockentry> | ||
<D:lockscope><D:shared/></D:lockscope> | ||
<D:locktype><D:write/></D:locktype> | ||
</D:lockentry> | ||
</D:supportedlock> | ||
<D:resourcetype>${prop.resourcetype}</D:resourcetype> | ||
</D:prop> | ||
<D:status>HTTP/1.1 200 OK</D:status> | ||
</D:propstat> | ||
</D:response>`; | ||
} | ||
|
||
function escapeXml(unsafe: string): string {replace(/[<>&'"]/g, function (c) { | ||
switch (c) { | ||
case '<': return '<'; | ||
case '>': return '>'; | ||
case '&': return '&'; | ||
case '\'': return '''; | ||
case '"': return '"'; | ||
} | ||
return c; | ||
}); | ||
} |