New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extended image checkings #15
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,10 +26,30 @@ | |
|
||
module powerbi.extensibility.utils.dataview { | ||
export module validationHelper { | ||
export function checkIsImageUrlAllowable(url: string): boolean { | ||
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions. | ||
// Also excludes directives "javascript:" and "data:". | ||
return (/\.(gif|jpg|png|svg)$/i).test(url) && !(/(javascript:|data:)/i).test(url); | ||
export function isImageUrlAllowed(url: string): boolean { | ||
// Excludes all URLs that don't contain .gif .jpg .png or .svg extensions and don't start from "http(s)://". | ||
// As a result -- also excludes all directives such as "javascript:", "data:" and "blob:". | ||
return (/^https?:\/\/.+\.(gif|jpg|png|svg)$/i).test(url); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm wondering why this RegExp excludes base64 encoded images? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we really need base64 images? |
||
} | ||
|
||
export function isFileImage(url: string, imageCheckResultCallBack: (isImage: boolean, contentType: string) => void) { | ||
let request = new XMLHttpRequest(); | ||
request.onreadystatechange = function () { | ||
if (request.readyState !== this.HEADERS_RECEIVED) { | ||
return; | ||
} | ||
|
||
let contentType = request.getResponseHeader("Content-Type"), | ||
supportedTypes = ["image/png", "image/jpeg", "image/gif", "image/svg+xml"]; | ||
|
||
if (supportedTypes.indexOf(contentType) > -1) { | ||
return imageCheckResultCallBack(true, contentType); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I suppose that it'd be nice to return image data as the third argument of callback. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't see how this info may be useful, especially if we use HEAD request There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It doesn't make a sense |
||
} | ||
|
||
return imageCheckResultCallBack(false, contentType); | ||
}; | ||
request.open("HEAD", url, true); | ||
request.send(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Plain text with some data taht is used in unit tests | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling mistake: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pattern allows us to use a regexp instead of specifying each image type independently.
Please write a RegExp that covers all of types that you want to using with unit tests.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to use something like (jpg|gif|png|svg), but it doesn't work. I will try regexp expression.