Skip to content

Commit cd76c88

Browse files
authored
Merge pull request #18 from OceanNetworksCanada/16-fix-download-issue-occurred-in-data-product-delivery-service
issue-16: fix download bug in data product delivery
2 parents b2901d0 + f2eddac commit cd76c88

File tree

2 files changed

+44
-30
lines changed

2 files changed

+44
-30
lines changed

onc/+onc/DataProductFile.m

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
uri = matlab.net.URI(this.baseUrl);
6565
uri.Query = matlab.net.QueryParameter(this.filters);
6666
fullUrl = char(uri);
67-
options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout, 'ConvertResponse', false);
67+
68+
options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout);
69+
6870
while this.status == 202
6971
% run and time request
7072
if this.showInfo, log.printLine(sprintf('Requesting URL:\n %s', fullUrl)); end
@@ -93,44 +95,45 @@
9395

9496
% Obtain filesize from headers, or fallback to body string length
9597
lengthData = response.getFields('Content-Length');
98+
[~, ~, ext] = fileparts(filename);
9699
if length(lengthData) == 1
97100
this.fileSize = str2double(lengthData.Value);
101+
elseif strcmp(ext, '.xml')
102+
this.fileSize = length(xmlwrite(response.Body.Data));
98103
else
99104
this.fileSize = strlength(response.Body.Data);
100105
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
113117
end
118+
114119
this.downloadingTime = round(duration, 3);
115120

116121
% log status
117122
if saveResult == 0
118123
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;
122124
end
123125
elseif s == 202
124126
% Still processing, wait and retry
125-
log.printResponse(jsondecode(response.Body.Data));
127+
log.printResponse(response.Body.Data);
126128
pause(pollPeriod);
127129
elseif s == 204
128130
% No data found
129131
log.printLine('No data found.\n');
130132
elseif s == 400
131133
% API Error
132134
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+
134137
elseif s == 404
135138
% Index too high, no more files to download
136139
else

onc/+util/save_as_file.m

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
% @param fileName Name of the file to save
77
% @param overwrite If true will overwrite files with the same name
88
% @return (numeric) Result code from {0: done, -1: error, -2: fileExists}
9-
function endCode = save_as_file(response, filePath, fileName, varargin)
9+
function endCode = save_as_file(dataToWrite, filePath, fileName, varargin)
1010
[overwrite] = util.param(varargin, 'overwrite', false);
1111

1212
fullPath = fileName;
@@ -25,25 +25,36 @@
2525
try
2626
matlabVersion = version('-release');
2727
year = str2double(matlabVersion(1:end-1));
28-
if year >= 2021
29-
f = fopen(fullPath, 'w','n','ISO-8859-1');
28+
[~, ~, ext] = fileparts(fileName);
29+
% if result is an image file or .xml file, use other save methods instead of fwrite.
30+
if strcmp(ext, '.png') || strcmp(ext, '.jpg')
31+
imwrite(dataToWrite, fullPath);
32+
elseif strcmp(ext, '.xml')
33+
xmlwrite(fullPath, dataToWrite);
3034
else
31-
f = fopen(fullPath, 'w','n');
35+
% open output file
36+
if year >= 2021
37+
f = fopen(fullPath, 'w','n','ISO-8859-1');
38+
else
39+
f = fopen(fullPath, 'w','n');
40+
end
41+
42+
% write result if open file successfully
43+
if f ~= -1
44+
fwrite(f, char(dataToWrite));
45+
else
46+
endCode = -1;
47+
return;
48+
end
49+
fclose(f);
3250
end
33-
34-
if f ~= -1
35-
fwrite(f, response);
36-
else
37-
endCode = -1;
38-
return;
39-
end
40-
fclose(f);
4151
catch ex
4252
disp(ex);
4353
endCode = -1;
4454
return;
4555
end
4656
else
57+
% if file exists and overwrite is false, raise exception
4758
throw(MException('onc:FileExistsError', 'Data product file exists in destination but overwrite is set to false'));
4859
end
4960

0 commit comments

Comments
 (0)