| 68 | const FILE_COPY_BUFFER_SIZE = 64 * 1024; |
| 69 | |
| 70 | export class DownloadTask { |
| 71 | private context: common.Context; |
| 72 | private hash: string; |
| 73 | private eventHub: EventHub; |
| 74 | |
| 75 | constructor(context: common.Context) { |
| 76 | this.context = context; |
| 77 | this.eventHub = EventHub.getInstance(); |
| 78 | } |
| 79 | |
| 80 | private async removeDirectory(path: string): Promise<void> { |
| 81 | try { |
| 82 | const res = fileIo.accessSync(path); |
| 83 | if (res) { |
| 84 | const stat = await fileIo.stat(path); |
| 85 | if (stat.isDirectory()) { |
| 86 | const files = await fileIo.listFile(path); |
| 87 | |
| 88 | const entries = files.filter(file => file !== '.' && file !== '..'); |
| 89 | const DELETE_CONCURRENCY = 32; |
| 90 | for (let i = 0; i < entries.length; i += DELETE_CONCURRENCY) { |
| 91 | const batch = entries.slice(i, i + DELETE_CONCURRENCY); |
| 92 | await Promise.all( |
| 93 | batch.map(file => this.removeDirectory(`${path}/${file}`)), |
| 94 | ); |
| 95 | } |
| 96 | await fileIo.rmdir(path); |
| 97 | } else { |
| 98 | await fileIo.unlink(path); |
| 99 | } |
| 100 | } |
| 101 | } catch (error) { |
| 102 | console.error('Failed to delete directory:', error); |
| 103 | throw error; |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | private async ensureDirectory(path: string): Promise<void> { |
| 108 | if (!path || fileIo.accessSync(path)) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | const parentPath = path.substring(0, path.lastIndexOf('/')); |
| 113 | if (parentPath && parentPath !== path) { |
| 114 | await this.ensureDirectory(parentPath); |
| 115 | } |
| 116 | |
| 117 | if (!fileIo.accessSync(path)) { |
| 118 | try { |
| 119 | await fileIo.mkdir(path); |
| 120 | } catch (error) { |
| 121 | if (!fileIo.accessSync(path)) { |
| 122 | throw error; |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 |
nothing calls this directly
no outgoing calls
no test coverage detected