(from, to, options)
| 571 | } |
| 572 | |
| 573 | async copy (from, to, options) { |
| 574 | if (overQuota(this.quotaFile, this.quota)) { |
| 575 | debug.handlers('COPY -- Over quota') |
| 576 | throw error(413, 'Storage quota exceeded') |
| 577 | } |
| 578 | |
| 579 | const originalParsedPath = urlModule.parse(from) |
| 580 | const parsedPath = urlModule.parse(to) |
| 581 | const fromPath = this.resourceMapper.resolveFilePath( |
| 582 | originalParsedPath.hostname, |
| 583 | decodeURIComponent(originalParsedPath.pathname) |
| 584 | ) |
| 585 | const toPath = this.resourceMapper.resolveFilePath( |
| 586 | parsedPath.hostname, |
| 587 | decodeURIComponent(parsedPath.pathname) |
| 588 | ) |
| 589 | |
| 590 | // Check if file already exists |
| 591 | if (fs.existsSync(toPath)) { |
| 592 | throw error(412, 'Target file already exists') |
| 593 | } |
| 594 | |
| 595 | let copyPromise |
| 596 | |
| 597 | // create destination directory if not exists |
| 598 | mkdirp(dirname(toPath)) |
| 599 | |
| 600 | // If original is a single file |
| 601 | if (!fromPath.endsWith('/')) { |
| 602 | copyPromise = new Promise((resolve, reject) => { |
| 603 | const readStream = fs.createReadStream(fromPath) |
| 604 | const writeStream = fs.createWriteStream(toPath) |
| 605 | readStream.on('error', function (err) { |
| 606 | debug.handlers('Error reading file: ' + err) |
| 607 | reject(error(500, err)) |
| 608 | }) |
| 609 | writeStream.on('error', function (err) { |
| 610 | debug.handlers('Error writing file: ' + err) |
| 611 | reject(error(500, err)) |
| 612 | }) |
| 613 | writeStream.on('finish', function () { |
| 614 | debug.handlers('Finished copying file') |
| 615 | resolve() |
| 616 | }) |
| 617 | readStream.pipe(writeStream) |
| 618 | }) |
| 619 | } else { |
| 620 | // If original is a folder, copy recursively |
| 621 | copyPromise = new Promise((resolve, reject) => { |
| 622 | exec(`cp -r "${fromPath}" "${toPath}"`, function (err) { |
| 623 | if (err) { |
| 624 | debug.handlers('Error copying directory: ' + err) |
| 625 | reject(error(500, err)) |
| 626 | } else { |
| 627 | debug.handlers('Finished copying directory') |
| 628 | resolve() |
| 629 | } |
| 630 | }) |
no test coverage detected