|
64 | 64 | uri = matlab.net.URI(this.baseUrl);
|
65 | 65 | uri.Query = matlab.net.QueryParameter(this.filters);
|
66 | 66 | fullUrl = char(uri);
|
67 |
| - options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout, 'ConvertResponse', false); |
| 67 | + |
| 68 | + options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout); |
| 69 | + |
68 | 70 | while this.status == 202
|
69 | 71 | % run and time request
|
70 | 72 | if this.showInfo, log.printLine(sprintf('Requesting URL:\n %s', fullUrl)); end
|
|
93 | 95 |
|
94 | 96 | % Obtain filesize from headers, or fallback to body string length
|
95 | 97 | lengthData = response.getFields('Content-Length');
|
| 98 | + [~, ~, ext] = fileparts(filename); |
96 | 99 | if length(lengthData) == 1
|
97 | 100 | this.fileSize = str2double(lengthData.Value);
|
| 101 | + elseif strcmp(ext, '.xml') |
| 102 | + this.fileSize = length(xmlwrite(response.Body.Data)); |
98 | 103 | else
|
99 | 104 | this.fileSize = strlength(response.Body.Data);
|
100 | 105 | end
|
101 |
| - |
102 |
| - savefilename = fullfile(outPath, filename); |
103 |
| - % Create folder if not exist |
104 |
| - if ~exist(outPath,'dir') |
105 |
| - mkdir(outPath) |
106 |
| - end |
107 |
| - % saveResult -2: File exists and set to not overwrite 0: done |
108 |
| - if ~overwrite && exist(savefilename,'file') == 2 |
109 |
| - saveResult = -2; |
110 |
| - else |
111 |
| - websave(savefilename,uri.EncodedURI); |
112 |
| - saveResult = 0; |
| 106 | + try |
| 107 | + saveResult = util.save_as_file(response.Body.Data, outPath, filename, 'overwrite', overwrite); |
| 108 | + catch ME |
| 109 | + if strcmp(ME.identifier, 'onc:FileExistsError') |
| 110 | + log.printLine(sprintf('Skipping "%s": File already exists\n', this.fileName)); |
| 111 | + this.status = 777; |
| 112 | + saveResult = -2; |
| 113 | + this.downloaded = false; |
| 114 | + else |
| 115 | + rethrow(ME); |
| 116 | + end |
113 | 117 | end
|
| 118 | + |
114 | 119 | this.downloadingTime = round(duration, 3);
|
115 | 120 |
|
116 | 121 | % log status
|
117 | 122 | if saveResult == 0
|
118 | 123 | log.printLine(sprintf('Downloaded "%s"\n', this.fileName));
|
119 |
| - elseif saveResult == -2 |
120 |
| - log.printLine(sprintf('Skipping "%s": File already exists\n', this.fileName)); |
121 |
| - this.status = 777; |
122 | 124 | end
|
123 | 125 | elseif s == 202
|
124 | 126 | % Still processing, wait and retry
|
125 |
| - log.printResponse(jsondecode(response.Body.Data)); |
| 127 | + log.printResponse(response.Body.Data); |
126 | 128 | pause(pollPeriod);
|
127 | 129 | elseif s == 204
|
128 | 130 | % No data found
|
129 | 131 | log.printLine('No data found.\n');
|
130 | 132 | elseif s == 400
|
131 | 133 | % API Error
|
132 | 134 | util.print_error(response, fullUrl);
|
133 |
| - throw(util.prepare_exception(s, double(jsondecode(response.Body.Data).errors.errorCode))); |
| 135 | + throw(util.prepare_exception(s, double(response.Body.Data.errors.errorCode))); |
| 136 | + |
134 | 137 | elseif s == 404
|
135 | 138 | % Index too high, no more files to download
|
136 | 139 | else
|
|
0 commit comments