flaskcode icon indicating copy to clipboard operation
flaskcode copied to clipboard

how to set allowed_extensions?

Open c2h2 opened this issue 5 years ago • 3 comments

Hi, Great project.

Can you tell us how to sepcify allowed_extensions in a flask app?

c2h2 avatar Dec 02 '20 08:12 c2h2

Currently this is not possible filter file types but this feature is in backlog and will be added in future development.

sujeetkv avatar Dec 04 '20 08:12 sujeetkv

Looking forward to this new release. I need to edit files with 'conf' type extensions.

eduardomsec avatar Sep 23 '22 12:09 eduardomsec

How to get rid of "This file type is not allowed to open..."

I digged into the source code and just found out a function called flaskcode.validResource() into file flaskcode.js, It's the core of file type validation.

This function try to find a syntax handler. If it can't, just raise the error. But it would be a lot more easy if it just handle it as plain/text ascii.

flaskcode.validResource = function (filename) {
    var ext = flaskcode.getExt(filename);
    var lang = ext ? flaskcode.getLanguageByExtension(ext) : null;
    return lang ? flaskcode.allowedLangIds.indexOf(lang.id) > -1 : false;
};

You can see at line #18 the default handler!

flaskcode.languages = [];
flaskcode.defaultExt = 'txt';
flaskcode.defaultLangId = 'plaintext';
flaskcode.defaultLang = null;
flaskcode.fallbackLang = null;

The return of flaskcode.getLanguageByExtension(ext) is something like this:

{
    "id": "python",
    "extensions": [
        ".py",
        ".rpy",
        ".pyw",
        ".cpy",
        ".gyp",
        ".gypi"
    ],
    "aliases": [
        "Python",
        "py"
    ],
    "firstLine": "^#!/.*\\bpython[0-9.-]*\\b"
}

This is my validResource() version: ⭐⭐ ⭐ ⭐ ⭐


flaskcode.validResource = function (filename) {
    var ext = flaskcode.getExt(filename);
    var lang = ext ? flaskcode.getLanguageByExtension(ext) : null;
    
    /* fallback to plaintext */
    if(!lang){ /* if not found try text file */
        lang = flaskcode.getLanguageByExtension('txt')
        //alert(JSON.stringify(lang, null, 4));
        if(lang){ /* Just to be sure! */
            if(ext) /* add ext to array */
               lang.extensions.push("."+ext);
            /* return the struct */
            return lang
        }
    }
    return lang ? flaskcode.allowedLangIds.indexOf(lang.id) > -1 : false;
};

💡💡💡 Also a good trick would be ignore all '.git' and '.env' folder! It can slow down the editor! 💡💡💡

💣💣 💣 Be carefull! If you try to open a binary file, things can get messy! 💣💣 💣

That's all Folks!!!

2023-07-03 17_57_29-FlaskCode - flaskcode 2023-07-03 17_57_44-FlaskCode - flaskcode

WillianBR avatar Jul 03 '23 20:07 WillianBR