Skip to content

Commit

Permalink
Merge pull request #18 from OceanNetworksCanada/16-fix-download-issue…
Browse files Browse the repository at this point in the history
…-occurred-in-data-product-delivery-service

issue-16: fix download bug in data product delivery
  • Loading branch information
IslaL authored Apr 18, 2024
2 parents b2901d0 + f2eddac commit cd76c88
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 30 deletions.
39 changes: 21 additions & 18 deletions onc/+onc/DataProductFile.m
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@
uri = matlab.net.URI(this.baseUrl);
uri.Query = matlab.net.QueryParameter(this.filters);
fullUrl = char(uri);
options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout, 'ConvertResponse', false);

options = matlab.net.http.HTTPOptions('ConnectTimeout', timeout);

while this.status == 202
% run and time request
if this.showInfo, log.printLine(sprintf('Requesting URL:\n %s', fullUrl)); end
Expand Down Expand Up @@ -93,44 +95,45 @@

% Obtain filesize from headers, or fallback to body string length
lengthData = response.getFields('Content-Length');
[~, ~, ext] = fileparts(filename);
if length(lengthData) == 1
this.fileSize = str2double(lengthData.Value);
elseif strcmp(ext, '.xml')
this.fileSize = length(xmlwrite(response.Body.Data));
else
this.fileSize = strlength(response.Body.Data);
end

savefilename = fullfile(outPath, filename);
% Create folder if not exist
if ~exist(outPath,'dir')
mkdir(outPath)
end
% saveResult -2: File exists and set to not overwrite 0: done
if ~overwrite && exist(savefilename,'file') == 2
saveResult = -2;
else
websave(savefilename,uri.EncodedURI);
saveResult = 0;
try
saveResult = util.save_as_file(response.Body.Data, outPath, filename, 'overwrite', overwrite);
catch ME
if strcmp(ME.identifier, 'onc:FileExistsError')
log.printLine(sprintf('Skipping "%s": File already exists\n', this.fileName));
this.status = 777;
saveResult = -2;
this.downloaded = false;
else
rethrow(ME);
end
end

this.downloadingTime = round(duration, 3);

% log status
if saveResult == 0
log.printLine(sprintf('Downloaded "%s"\n', this.fileName));
elseif saveResult == -2
log.printLine(sprintf('Skipping "%s": File already exists\n', this.fileName));
this.status = 777;
end
elseif s == 202
% Still processing, wait and retry
log.printResponse(jsondecode(response.Body.Data));
log.printResponse(response.Body.Data);
pause(pollPeriod);
elseif s == 204
% No data found
log.printLine('No data found.\n');
elseif s == 400
% API Error
util.print_error(response, fullUrl);
throw(util.prepare_exception(s, double(jsondecode(response.Body.Data).errors.errorCode)));
throw(util.prepare_exception(s, double(response.Body.Data.errors.errorCode)));

elseif s == 404
% Index too high, no more files to download
else
Expand Down
35 changes: 23 additions & 12 deletions onc/+util/save_as_file.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
% @param fileName Name of the file to save
% @param overwrite If true will overwrite files with the same name
% @return (numeric) Result code from {0: done, -1: error, -2: fileExists}
function endCode = save_as_file(response, filePath, fileName, varargin)
function endCode = save_as_file(dataToWrite, filePath, fileName, varargin)
[overwrite] = util.param(varargin, 'overwrite', false);

fullPath = fileName;
Expand All @@ -25,25 +25,36 @@
try
matlabVersion = version('-release');
year = str2double(matlabVersion(1:end-1));
if year >= 2021
f = fopen(fullPath, 'w','n','ISO-8859-1');
[~, ~, ext] = fileparts(fileName);
% if result is an image file or .xml file, use other save methods instead of fwrite.
if strcmp(ext, '.png') || strcmp(ext, '.jpg')
imwrite(dataToWrite, fullPath);
elseif strcmp(ext, '.xml')
xmlwrite(fullPath, dataToWrite);
else
f = fopen(fullPath, 'w','n');
% open output file
if year >= 2021
f = fopen(fullPath, 'w','n','ISO-8859-1');
else
f = fopen(fullPath, 'w','n');
end

% write result if open file successfully
if f ~= -1
fwrite(f, char(dataToWrite));
else
endCode = -1;
return;
end
fclose(f);
end

if f ~= -1
fwrite(f, response);
else
endCode = -1;
return;
end
fclose(f);
catch ex
disp(ex);
endCode = -1;
return;
end
else
% if file exists and overwrite is false, raise exception
throw(MException('onc:FileExistsError', 'Data product file exists in destination but overwrite is set to false'));
end

Expand Down

0 comments on commit cd76c88

Please sign in to comment.