Add WebDav server, fully functional!
Some more code cleanup.
This commit is contained in:
@@ -213,79 +213,6 @@ export function convertObjectToFormData(formDataObject: Record<string, any>): Fo
|
||||
return formData;
|
||||
}
|
||||
|
||||
function writeXmlTag(tagName: string, value: any, attributes?: Record<string, any>) {
|
||||
const attributesXml = attributes
|
||||
? Object.keys(attributes || {}).map((attributeKey) => `${attributeKey}="${escapeHtml(attributes[attributeKey])}"`)
|
||||
.join(' ')
|
||||
: '';
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (value.length === 0) {
|
||||
return `<${tagName}${attributesXml ? ` ${attributesXml}` : ''} />`;
|
||||
}
|
||||
|
||||
const xmlLines: string[] = [];
|
||||
|
||||
for (const valueItem of value) {
|
||||
xmlLines.push(writeXmlTag(tagName, valueItem));
|
||||
}
|
||||
|
||||
return xmlLines.join('\n');
|
||||
}
|
||||
|
||||
if (typeof value === 'object') {
|
||||
if (Object.keys(value).length === 0) {
|
||||
return `<${tagName}${attributesXml ? ` ${attributesXml}` : ''} />`;
|
||||
}
|
||||
|
||||
return `<${tagName}${attributesXml ? ` ${attributesXml}` : ''}>${convertObjectToDavXml(value)}</${tagName}>`;
|
||||
}
|
||||
|
||||
return `<${tagName}${attributesXml ? ` ${attributesXml}` : ''}>${value}</${tagName}>`;
|
||||
}
|
||||
|
||||
export function convertObjectToDavXml(davObject: Record<string, any>, isInitial = false): string {
|
||||
const xmlLines: string[] = [];
|
||||
|
||||
if (isInitial) {
|
||||
xmlLines.push(`<?xml version="1.0" encoding="UTF-8"?>`);
|
||||
}
|
||||
|
||||
for (const key of Object.keys(davObject)) {
|
||||
if (key.endsWith('_attributes')) {
|
||||
continue;
|
||||
}
|
||||
|
||||
xmlLines.push(writeXmlTag(key, davObject[key], davObject[`${key}_attributes`]));
|
||||
}
|
||||
|
||||
return xmlLines.join('\n');
|
||||
}
|
||||
|
||||
function addLeadingZero(number: number) {
|
||||
if (number < 10) {
|
||||
return `0${number}`;
|
||||
}
|
||||
|
||||
return number.toString();
|
||||
}
|
||||
|
||||
export function buildRFC822Date(dateString: string) {
|
||||
const dayStrings = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
|
||||
const monthStrings = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
|
||||
|
||||
const timeStamp = Date.parse(dateString);
|
||||
const date = new Date(timeStamp);
|
||||
|
||||
const day = dayStrings[date.getDay()];
|
||||
const dayNumber = addLeadingZero(date.getUTCDate());
|
||||
const month = monthStrings[date.getUTCMonth()];
|
||||
const year = date.getUTCFullYear();
|
||||
const time = `${addLeadingZero(date.getUTCHours())}:${addLeadingZero(date.getUTCMinutes())}:00`;
|
||||
|
||||
return `${day}, ${dayNumber} ${month} ${year} ${time} +0000`;
|
||||
}
|
||||
|
||||
export const capitalizeWord = (string: string) => {
|
||||
return `${string.charAt(0).toLocaleUpperCase()}${string.slice(1)}`;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user