-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Javascript to fetch mimetype icons #16724
Conversation
mimeTypeIcons: {}, | ||
|
||
init: function() { | ||
OC.MimeType.mimeTypeAlias = { |
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.
Shouldn't this be generated from
Line 57 in 246000f
private static $mimeTypeAlias = array( |
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.
Is this then even needed in the PHP code part?
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'm not sure. if It is needed then on the php side... we'd have to check.
And yes they should be generated from one source if both are needed.
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.
Probably we want something that makes a lot of the get requets unneeded by including which files are available as mimetype and do almost everything just in javascript.
But probably such improvements are for a later PR
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.
The mapping is needed, otherwise we don't know which media types belongs to which icon and you end up with a bitmap icon instead of a vector icon for an eps file per example.
If both the JS and PHP files need the same mappings, maybe it would be wise to implement #15384 at the same time and cache those files? |
Not sure if related, but have you seen this oldie https://github.com/owncloud/core/blob/master/apps/files/js/files.js#L152 |
@PVince81 - The problem with that method is that it makes an extra request to the server. EDIT: I see now in the code that checkExists() does the same thing... :( |
return OC.MimeType.mimeTypeIcons[mimeType]; | ||
} | ||
|
||
if (mimeType == 'dir') { |
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.
Use ===
I don't think it's a good idea to duplicate the list we have in PHP and have it again in JS. |
}, | ||
|
||
mimetypeIcon: function(mimeType) { | ||
if (mimeType === undefined) { |
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.
use _.isUndefined(mimeType)
which is more accurate. (sometimes equalities in JS are weird)
Ok, so my quick hack obviously has even more problems than I quickly noticed. I'll try to script a file together that generates a json file which should make any access to the server unneeded. |
Ok now it just loads 1 big json file. Which makes the calls for each mimetype to the server obsolete. |
@PVince81 I do not assume we can count on the init of the function being completed before the first call to mimetypeIcon? Any nice javascript magic (that does not make my eyes bleed) that can help? |
O and where should this mimetypes.json file be stored? right now it is in /core |
@rullzer Who would be calling |
What you could do is add the callback as an argument to the mimetype method and call that once you've retrieved the file. Caching it would make sure you only make the call the first time. EDIT: Scratch that, mimetype is to get the path. It's best to have another call to init first. |
As for the location, maybe move it to config, but that would mean renaming the file to mimetype.aliases.js (or map), so that it doesn't conflict with that move #15384 The reason to not keep it in core would be that as soon as people start adding their own media types, they will want to change the mapping as well. |
If we don't want to load mimetypes asynchronously, which brings some issues, another idea is to compile a Javascript file with all mimetypes in it. Then just load that file with OC.Mimetypes = [
// list of mimetypes
] This is how it's done currently with L10N files to avoid extra async ajax load. Anyway, I think this is too big/risky a change for 8.1, moving to 8.2 |
Is there a way to fix this without introducing new APIs ? |
I introduced the icon into OCS ... this PR would like to remove it again to not clutter OCS even more with useless code. :( |
@PVince81 yeah that suggestions sounds like the best solution. Probably a simple script to generate this file is easiest. I guess the change is indeed a bit to big for 8.1... :( |
@owncloud-bot retest this please |
@@ -0,0 +1,68 @@ | |||
{ | |||
"_comment" : "When this file is changed be sure to run", | |||
"_comment2": ".occ maintenance:mimetypesjs", |
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.
.occ
=> ./occ
Now, how do we trigger the regeneration on every oC update? There may be new files installed with the update, so I guess some sort of always-on migration step will suffice? |
@Xenopathic lets look at that for another PR and just get this in for now :) |
A "repair step" could be written that calls the same API as the command, yes. Maybe that command's code should be exposed as a core public API instead to be callable from other places ? Does this also support that apps provide their own mime types ? If yes, then it means apps would also need an API to be able to regenerate the file. |
@PVince81 no this is not yet supported. Lets also look at that for another PR |
This makes it possible to retrieve the icon for mimetypes in javascript. It makes no additional queries to the server to retrieve the mimetype. * config/mimetypealiases.json added * mimetype.js: this is where the logic resides to convert from mimetype to icon url * mimetypelist.js: generated file with a list of mimetype mapping (aliases) and the list of icon files * ./occ maintenance:mimetypesjs : new command for occ to gernerate mimetypes.js * unit tests updated and still work * javascript tests added * theming support * folder of the theme is now present in javascript (OC.theme.folder)
Switch to new javascript mimetype resolver
A new inspection was created. |
One more rebase because I can't type. |
@owncloud-bot retest this please |
expect(res).toEqual(OC.webroot + '/core/img/filetypes/file.svg'); | ||
}); | ||
|
||
it('test if the cache works correctly', function() { |
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.
of course it tests because it's a test 😉
For next time (leave it now), this would be "caches the mimetype icons" (this is the behavior)
👍 |
1 similar comment
👍 (I'm too scared to merge this beast though 🙈 ) |
Javascript to fetch mimetype icons
@Xenopathic I'm not ;) |
OMG IT BROKE EVERYTHING! WE SHOULD NOT HAVE MERGED THIS!!!! Just kidding. Cool stuff 🚀 |
As stated in #16531 we do not want to have the icon in the return of the OCS Share API.
This PR adds a mimetype.js file that is basically and implentation of the mimetype stuff from OC_Helper in javascript. It can probably be made more efficient later on.
The second commit removes the icon from the OCS Share API.
Todo:
@MorrisJobke something like this is what you had in mind as well I assume?
@PVince81 could you also have a look
@oparoz Probably also relevant for #14431