[#44856] replaced regex in upload response parsing

- use DOMParser instead of regex
- fixed linter for type unions and intersections
pull/11837/head
Eric Schubert 2 years ago
parent e03bc41a94
commit 024b60fbda
No known key found for this signature in database
GPG Key ID: 1D346C019BD4BAA2
  1. 3
      frontend/.eslintrc.js
  2. 34
      frontend/src/app/shared/components/storages/services/upload-storage-files.service.ts
  3. 10
      frontend/src/app/shared/components/storages/storage/storage.component.ts

@ -125,6 +125,9 @@ module.exports = {
},
],
// Allow writing type union and type intersections without space
"@typescript-eslint/space-infix-ops": "off",
// Allow empty interfaces for naming purposes (HAL resources)
"@typescript-eslint/no-empty-interface": "off",

@ -72,18 +72,19 @@ export class UploadStorageFilesService {
}
private parseXmlResponse(xml:string):IStorageFile {
const response = new DOMParser().parseFromString(xml, 'application/xml');
const error = new Error(`Invalid response for uploaded file: ${xml}`);
const id = /<oc:fileid>(.*)<\/oc:fileid>/.exec(xml)?.pop();
const id = response.getElementsByTagName('oc:fileid')[0].textContent;
if (!id) { throw error; }
const mimeType = /<d:getcontenttype>(.*)<\/d:getcontenttype>/.exec(xml)?.pop();
const mimeType = response.getElementsByTagName('d:getcontenttype')[0].textContent;
if (!mimeType) { throw error; }
const size = /<oc:size>(.*)<\/oc:size>/.exec(xml)?.pop();
const size = response.getElementsByTagName('oc:size')[0].textContent;
if (!size) { throw error; }
const href = /<d:href>(.*)<\/d:href>/.exec(xml)?.pop();
const href = response.getElementsByTagName('d:href')[0].textContent;
const parts = href?.split('/');
if (!parts || parts.length < 1) { throw error; }
@ -92,12 +93,12 @@ export class UploadStorageFilesService {
const location = `/${parts.slice(parts.indexOf('webdav') + 1).join('/')}`;
const date = /<d:getlastmodified>(.*)<\/d:getlastmodified>/.exec(xml)?.pop();
const date = response.getElementsByTagName('d:getlastmodified')[0].textContent;
if (!date) { throw error; }
const createdAt = this.timezoneService.parseDatetime(date).toISOString();
const lastModifiedAt = createdAt;
const creator = /<oc:owner-display-name>(.*)<\/oc:owner-display-name>/.exec(xml)?.pop();
const creator = response.getElementsByTagName('oc:owner-display-name')[0].textContent;
if (!creator) { throw error; }
return {
@ -114,16 +115,15 @@ export class UploadStorageFilesService {
}
private get propfindBody() {
return `
<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<oc:fileid />
<d:getlastmodified />
<d:getcontenttype />
<oc:size />
<oc:owner-display-name />
</d:prop>
</d:propfind>`;
return `<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns" xmlns:nc="http://nextcloud.org/ns">
<d:prop>
<oc:fileid />
<d:getlastmodified />
<d:getcontenttype />
<oc:size />
<oc:owner-display-name />
</d:prop>
</d:propfind>`;
}
}

@ -142,7 +142,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private get addFileLinksHref() {
return (this.resource.$links as unknown & { addFileLink:IHalResourceLink }).addFileLink.href;
return (this.resource.$links as unknown&{ addFileLink:IHalResourceLink }).addFileLink.href;
}
constructor(
@ -185,7 +185,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
this.allowEditing$ = this
.currentUserService
.hasCapabilities$('file_links/manage', (this.resource.project as unknown & { id:string }).id);
.hasCapabilities$('file_links/manage', (this.resource.project as unknown&{ id:string }).id);
document.body.addEventListener('dragover', this.onGlobalDragOver.bind(this));
document.body.addEventListener('dragleave', this.afterGlobalDragEnd.bind(this));
@ -231,7 +231,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
this.openSelectLocationDialog(fileList);
}
private openSelectLocationDialog(files:FileList | null):void {
private openSelectLocationDialog(files:FileList|null):void {
const locals = {
storageType: this.storage._links.type.href,
storageTypeName: this.storageType,
@ -284,7 +284,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private uploadResourceLink(fileName:string, location:string):IPrepareUploadLink {
const project = (this.resource.project as unknown & { id:string }).id;
const project = (this.resource.project as unknown&{ id:string }).id;
const link = this.storage._links.prepareUpload.filter((value) => project === value.payload.projectId.toString());
if (link.length === 0) {
throw new Error('Cannot upload to this storage. Missing permissions in project.');
@ -369,7 +369,7 @@ export class StorageComponent extends UntilDestroyedMixin implements OnInit, OnD
}
private get fileLinkSelfLink():string {
const fileLinks = this.resource.fileLinks as unknown & { href:string };
const fileLinks = this.resource.fileLinks as unknown&{ href:string };
return `${fileLinks.href}?filters=[{"storage":{"operator":"=","values":["${this.storage.id}"]}}]`;
}

Loading…
Cancel
Save