Skip to content
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

Merged
merged 2 commits into from
Jul 6, 2015
Merged

Javascript to fetch mimetype icons #16724

merged 2 commits into from
Jul 6, 2015

Conversation

rullzer
Copy link
Contributor

@rullzer rullzer commented Jun 3, 2015

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:

  • Tests!

@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

@rullzer rullzer added this to the 8.1-current milestone Jun 3, 2015
mimeTypeIcons: {},

init: function() {
OC.MimeType.mimeTypeAlias = {
Copy link
Contributor

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

private static $mimeTypeAlias = array(
or vice versa or both use a common JSON file?

Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Contributor

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.

@oparoz
Copy link
Contributor

oparoz commented Jun 3, 2015

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?

@PVince81
Copy link
Contributor

PVince81 commented Jun 3, 2015

Not sure if related, but have you seen this oldie https://github.com/owncloud/core/blob/master/apps/files/js/files.js#L152

@oparoz
Copy link
Contributor

oparoz commented Jun 3, 2015

@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') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ===

@PVince81
Copy link
Contributor

PVince81 commented Jun 3, 2015

I don't think it's a good idea to duplicate the list we have in PHP and have it again in JS.
But what could be done is have a JSON file that contains the mime types mapping, and load that JSON file from both PHP and JS.

},

mimetypeIcon: function(mimeType) {
if (mimeType === undefined) {
Copy link
Contributor

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)

@rullzer
Copy link
Contributor Author

rullzer commented Jun 3, 2015

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.

@rullzer
Copy link
Contributor Author

rullzer commented Jun 3, 2015

Ok now it just loads 1 big json file. Which makes the calls for each mimetype to the server obsolete.

@rullzer
Copy link
Contributor Author

rullzer commented Jun 3, 2015

@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?

@rullzer
Copy link
Contributor Author

rullzer commented Jun 3, 2015

O and where should this mimetypes.json file be stored? right now it is in /core

@PVince81
Copy link
Contributor

PVince81 commented Jun 3, 2015

@rullzer $.getJSON() is asynchronous, which means that when init() ends, the call isn't done yet.
There is no good way to make ajax-loaded stuff available in the main thread.

Who would be calling init() usually ?

@oparoz
Copy link
Contributor

oparoz commented Jun 3, 2015

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.

@rullzer
Copy link
Contributor Author

rullzer commented Jun 3, 2015

@PVince81 yeah that was I ws afraid of. It is now just called when the document is ready.

@oparoz I taught about that as well. However then you do run the risk that multiple instances simultaneously try to obtain the json file. Although that chance is probably pretty small

@oparoz
Copy link
Contributor

oparoz commented Jun 3, 2015

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.

@PVince81
Copy link
Contributor

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 addScript().

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

@PVince81 PVince81 modified the milestones: 8.2-next, 8.1-current Jun 18, 2015
@MorrisJobke
Copy link
Contributor

As stated in #16531 we do not want to have the icon in the return of the OCS Share API.

@PVince81 That was the goal for 8.1, because it was introduced in 8.1.

@PVince81
Copy link
Contributor

Is there a way to fix this without introducing new APIs ?

@MorrisJobke
Copy link
Contributor

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. :(

@rullzer
Copy link
Contributor Author

rullzer commented Jun 22, 2015

@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... :(

@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

@owncloud-bot retest this please

@@ -0,0 +1,68 @@
{
"_comment" : "When this file is changed be sure to run",
"_comment2": ".occ maintenance:mimetypesjs",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.occ => ./occ

@RobinMcCorkell
Copy link
Member

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?

@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

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 :)

@PVince81
Copy link
Contributor

PVince81 commented Jul 6, 2015

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.

@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

@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
@scrutinizer-notifier
Copy link

A new inspection was created.

@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

One more rebase because I can't type.

@ghost
Copy link

ghost commented Jul 6, 2015

💣 Test FAILed. 💣
nooo432

@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

@owncloud-bot retest this please

expect(res).toEqual(OC.webroot + '/core/img/filetypes/file.svg');
});

it('test if the cache works correctly', function() {
Copy link
Contributor

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)

@PVince81
Copy link
Contributor

PVince81 commented Jul 6, 2015

👍

@ghost
Copy link

ghost commented Jul 6, 2015

🚀 Test PASSed.🚀
chuck

1 similar comment
@ghost
Copy link

ghost commented Jul 6, 2015

🚀 Test PASSed.🚀
chuck

@RobinMcCorkell
Copy link
Member

👍 (I'm too scared to merge this beast though 🙈 )

rullzer pushed a commit that referenced this pull request Jul 6, 2015
Javascript to fetch mimetype icons
@rullzer rullzer merged commit b4f782b into master Jul 6, 2015
@rullzer rullzer deleted the mimetype-js branch July 6, 2015 19:00
@rullzer
Copy link
Contributor Author

rullzer commented Jul 6, 2015

@Xenopathic I'm not ;)
Luckily it is not yet used in a lot of places :)

@LukasReschke
Copy link
Member

OMG IT BROKE EVERYTHING! WE SHOULD NOT HAVE MERGED THIS!!!!

Just kidding. Cool stuff 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants