|
11 | 11 | % |
12 | 12 | % input: |
13 | 13 | % hyperlink: if a single input is provided, the function check if it is |
14 | | -% a hyperlink starting with http:// or https://, if so, it |
15 | | -% trys to extract the database name, document name and file |
16 | | -% name using NeuroJSON's standard link format as |
| 14 | +% a hyperlink starting with http://, https:// or ftp://, if |
| 15 | +% so, it trys to extract the database name, document name and |
| 16 | +% file name using NeuroJSON's standard link format as |
17 | 17 | % |
18 | 18 | % https://neurojson.org/io/stat.cgi?dbname=..&docname=..&file=..&size=.. |
19 | 19 | % |
20 | | -% if the string does not contain a link, it is treated as a |
21 | | -% local file path |
| 20 | +% if the URL does not follow the above format, a SHA-256 hash |
| 21 | +% will be computed based on the full URL to produce filename; |
| 22 | +% dbname is set as the first 2 letters of the hash and |
| 23 | +% docname is set to the 3rd/4th letters of the hash; the |
| 24 | +% domain name is also extracted from the URL; if the URL |
| 25 | +% contains the file's suffix, it is appended to the filename. |
| 26 | +% |
| 27 | +% if the string does not contain a link, or the link starts |
| 28 | +% with file://, it is treated as a local file path |
22 | 29 | % dbname: the name of the NeuroJSON database (must exist) |
23 | 30 | % docname: the name of the NeuroJSON dataset document (must exist) |
24 | 31 | % filename: the name of the data file - may contain a relative folder |
25 | | -% domain: optional, if not given, 'io' is used; otherwise, user can |
| 32 | +% domain: optional, if not given, 'default' is used; otherwise, user can |
26 | 33 | % specify customized domain name |
27 | 34 | % |
28 | 35 | % output: |
|
46 | 53 | % if a global variable NEUROJSON_CACHE is set in 'base', it will be |
47 | 54 | % used instead of the above search paths |
48 | 55 | % |
| 56 | +% |
| 57 | +% example: |
| 58 | +% [cachepath, filename] = jsoncache('https://neurojson.org/io/stat.cgi?action=get&db=openneuro&doc=ds000001&file=sub-01/anat/sub-01_inplaneT2.nii.gz&size=669578') |
| 59 | +% [cachepath, filename] = jsoncache('https://raw.githubusercontent.com/fangq/jsonlab/master/examples/example1.json') |
| 60 | +% [cachepath, filename] = jsoncache('https://neurojson.io:7777/adhd200/Brown') |
| 61 | +% [cachepath, filename] = jsoncache('https://neurojson.io:7777/openneuro/ds003805') |
| 62 | +% |
49 | 63 | % -- this function is part of iso2mesh toolbox (http://iso2mesh.sf.net) |
50 | 64 | % |
51 | 65 |
|
|
66 | 80 | end |
67 | 81 |
|
68 | 82 | if (nargin < 4) |
69 | | - domain = 'io'; |
| 83 | + domain = 'default'; |
70 | 84 | end |
71 | 85 |
|
72 | 86 | if (nargin == 1) |
73 | 87 | link = dbname; |
74 | | - if (isempty(regexp(link, '://', 'once'))) |
75 | | - filename = link; |
| 88 | + if (~isempty(regexp(link, '^file://', 'once')) || isempty(regexp(link, '://', 'once'))) |
| 89 | + filename = regexprep(link, '^file://', ''); |
76 | 90 | if (exist(filename, 'file')) |
77 | 91 | cachepath = filename; |
78 | 92 | filename = true; |
|
81 | 95 | else |
82 | 96 | if (~isempty(regexp(link, '^https*://neurojson.org/io/', 'once'))) |
83 | 97 | domain = 'io'; |
84 | | - end |
85 | | - dbname = regexp(link, '(?<=db=)[^&]+', 'match'); |
86 | | - if (~isempty(dbname)) |
87 | | - dbname = dbname{1}; |
88 | 98 | else |
89 | | - dbname = ''; |
| 99 | + newdomain = regexprep(regexp(link, '^(https*|ftp)://[^\/?#:]+', 'match', 'once'), '^(https*|ftp)://', ''); |
| 100 | + if (~isempty(newdomain)) |
| 101 | + domain = newdomain; |
| 102 | + end |
90 | 103 | end |
91 | | - docname = regexp(link, '(?<=doc=)[^&]+', 'match'); |
92 | | - if (~isempty(docname)) |
93 | | - docname = docname{1}; |
94 | | - else |
95 | | - docname = ''; |
| 104 | + dbname = regexp(link, '(?<=db=)[^&]+', 'match', 'once'); |
| 105 | + docname = regexp(link, '(?<=doc=)[^&]+', 'match', 'once'); |
| 106 | + filename = regexp(link, '(?<=file=)[^&]+', 'match', 'once'); |
| 107 | + if (isempty(filename) && strcmp(domain, 'neurojson.io')) |
| 108 | + ref = regexp(link, '^(https*|ftp)://neurojson.io(:\d+)*(?<dbname>/[^\/]+)(?<docname>/[^\/]+)(?<filename>/[^\/?]+)*', 'names', 'once'); |
| 109 | + if (~isempty(ref)) |
| 110 | + if (~isempty(ref.dbname)) |
| 111 | + dbname = ref.dbname(2:end); |
| 112 | + end |
| 113 | + if (~isempty(ref.docname)) |
| 114 | + docname = ref.docname(2:end); |
| 115 | + end |
| 116 | + if (~isempty(ref.filename)) |
| 117 | + filename = ref.filename(2:end); |
| 118 | + elseif (~isempty(dbname)) |
| 119 | + if (~isempty(docname)) |
| 120 | + filename = [docname '.json']; |
| 121 | + else |
| 122 | + filename = [dbname '.json']; |
| 123 | + end |
| 124 | + end |
| 125 | + end |
96 | 126 | end |
97 | | - filename = regexp(link, '(?<=file=)[^&]+', 'match'); |
98 | | - if (~isempty(filename)) |
99 | | - filename = filename{1}; |
100 | | - else |
101 | | - filename = ''; |
| 127 | + if (isempty(filename)) |
| 128 | + filename = jdatahash(link); |
| 129 | + suffix = regexp(link, '\.\w{1,5}(?=([#&].*)*$)', 'match', 'once'); |
| 130 | + filename = [filename suffix]; |
| 131 | + if (isempty(dbname)) |
| 132 | + dbname = filename(1:2); |
| 133 | + end |
| 134 | + if (isempty(docname)) |
| 135 | + docname = filename(3:4); |
| 136 | + end |
102 | 137 | end |
103 | 138 | end |
104 | 139 | end |
|
0 commit comments