Skip to content

Commit a35bc86

Browse files
author
p01
committed
Clean up of resource_util.js
Moved the guess_type() and get_meta_data() to resource_util.js Use these in the cls.NetworkLoggerEntryPrototype
1 parent cdd096f commit a35bc86

File tree

2 files changed

+27
-54
lines changed

2 files changed

+27
-54
lines changed

src/network/network_service.js

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,14 +1037,10 @@ cls.NetworkLoggerEntryPrototype = function()
10371037
// The first guess is made based on file extension. No response is needed for that.
10381038
// The current response is updated though, at the time it will be the correct one.
10391039
// Multiple responses can get different types in this way.
1040-
if (!cls || !cls.ResourceUtil)
1040+
if (!cls || !cls.ResourceUtil || !cls.ResourceUtil.guess_type)
10411041
return;
10421042

1043-
// For "application/octet-stream" we check by extension even though we have a mime
1044-
if (!this.mime || this.mime.toLowerCase() === "application/octet-stream")
1045-
this.type = cls.ResourceUtil.extension_type_map[this.extension];
1046-
else
1047-
this.type = cls.ResourceUtil.mime_to_type(this.mime);
1043+
this.type = cls.ResourceUtil.guess_type(this.mime, this.extension);
10481044

10491045
if (this._current_response)
10501046
{

src/resource-manager/resource_util.js

Lines changed: 25 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,6 @@ cls.ResourceUtil.mime_type_map = {
144144
"application/x-silverlight-app": "silverlight"
145145
}
146146

147-
cls.ResourceUtil.type_to_string_map = {
148-
"css": "CSS",
149-
"pdf": "PDF",
150-
"postscript": "PostScript"
151-
};
152-
153147
/**
154148
* Returns the most sensible way of getting this resource,
155149
* as datauri or string, based on the mime type.
@@ -188,55 +182,38 @@ cls.ResourceUtil.mime_to_type = function(mime, extension)
188182
}
189183
}
190184

191-
cls.ResourceUtil.path_to_type = function(path)
185+
/**
186+
* Returns the most sensible type based on
187+
* the mimeType and extension of resource
188+
*/
189+
cls.ResourceUtil.guess_type = function(mime, extension)
192190
{
193-
if (path)
194-
{
195-
var extension = path.slice(path.lastIndexOf(".") + 1).toLowerCase();
196-
var query = extension.indexOf("?");
197-
if (query != -1)
198-
extension = extension.slice(0, query);
199-
var hash = extension.indexOf("#");
200-
if (hash != -1)
201-
extension = extension.slice(0, hash);
202-
return extension && this.extension_type_map[extension];
203-
}
204-
}
191+
// For "application/octet-stream" we check by extension even though we have a mime
192+
if (!mime || mime.toLowerCase() === "application/octet-stream")
193+
return this.extension_type_map[extension];
205194

206-
cls.ResourceUtil.url_path = function(url)
207-
{
208-
if (!url) { return null; }
209-
var firstslash = url.replace("://", "xxx").indexOf("/");
210-
var querystart = url.indexOf("?");
211-
if (querystart == -1) { querystart = url.length; }
212-
var path = url.slice(firstslash, querystart);
213-
return path;
195+
return this.mime_to_type(mime);
214196
}
215197

216-
cls.ResourceUtil.url_filename = function(url)
198+
/**
199+
* Returns meta_data of a cls.ResourceInfo
200+
*/
201+
cls.ResourceUtil.get_meta_data = function(resourceInfo)
217202
{
218-
var path = cls.ResourceUtil.url_path(url);
219-
var lastslash = path.lastIndexOf("/");
220-
if (
221-
lastslash === -1 || // no slash or
222-
path === "/" // the path _is_ a slash means there is no file name
223-
)
203+
var data = resourceInfo.data;
204+
if (!data)
205+
return;
206+
207+
if (resourceInfo.type == 'image')
224208
{
225-
return null;
209+
var i=new Image();
210+
i.src=data.content.stringData;
211+
if (i.naturalWidth)
212+
return i.naturalWidth + '\u00D7' + i.naturalHeight;
213+
else
214+
return 'vectorial image';
226215
}
227-
else {
228-
return path.slice(lastslash+1);
229-
}
230-
}
231-
232-
cls.ResourceUtil.url_host = function(url)
233-
{
234-
if (!url) { return null; }
235-
var host = url.replace(/\w+?:\/\//, "");
236-
var firstslash = host.indexOf("/");
237-
host = host.slice(0, firstslash == -1 ? host.length : firstslash);
238-
return host;
239-
}
216+
};
240217

241218
cls.ResourceUtil.header_presets = [
242219
{name: ui_strings.S_NETWORK_HEADER_OVERRIDES_PRESET_NONE, headers: ""},

0 commit comments

Comments
 (0)