How to link files directly from Github (raw.github.com)

后端 未结 10 2151
耶瑟儿~
耶瑟儿~ 2020-11-30 19:52

Are we allowed to link files directly from Github ?



        
相关标签:
10条回答
  • 2020-11-30 20:16

    If your webserver has active allow_url_include, GitHub serving the files as raw plain/text is not a problem since you can include the file first in a PHP script and modify its Headers to the proper MIME type.

    0 讨论(0)
  • 2020-11-30 20:19

    For those who ended up in this post and just want to get the raw link from an image in GitHub:

    If it is the case of an image, you can just add '?raw=true' at the end of the link to the file. E.g. Original link: https://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png

    Raw link: https://github.com/githubusername/repo_name/blob/master/20160309_212617-1.png?raw=true

    0 讨论(0)
  • 2020-11-30 20:22

    You can link directly to raw files, but it's best not to do it since the raw files always get sent with a plain/text header and can cause loading problems.

    0 讨论(0)
  • 2020-11-30 20:25

    You can use external server rawgithub.com. Just remove a dot between words 'raw' and 'github' https://raw.github.com/.. => https://rawgithub.com/ and use it. More info you find in this question.

    However, according to the rawgithub website it will be shutting down at the end of October 2019.

    0 讨论(0)
  • 2020-11-30 20:26

    After searching for this same functionality, I ended up writing my own PHP script to act as a proxy. The trouble I kept running into is even when you get the RAW version/link from Github and link to it in your own page, the header sent over was 'text/plain' and Chrome was not executing my JavaScript file from Github. I also didn't like the other links posted for using third party services because of the obvious security/tampering issues possible.

    So using this script, I can pass over the RAW link from Github, have the script set the correct headers, and then output the file as if it were coming from my own server. This script can also be used with a secure application to pull in non-secure scripts without throwing SSL errors warning of "Non-secure links used".

    Linking:

    <script src="proxy.php?link=https://raw.githubusercontent.com/UserName/repo/master/my_script.js"></script>

    proxy.php

    <?php
    ###################################################################################################################
    # 
    # This script can take two URL variables
    # 
    # "type"
    #   OPTIONAL
    #   STRING
    #   Sets the type of file that is output
    # 
    # "link"
    #   REQUIRED
    #   STRING
    #   The link to grab and output through this proxy script
    # 
    ###################################################################################################################
    
    
    
    # First we need to set the headers for the output file
    # So check to see if the type is specified first and if so, then set according to what is being requested
    if(isset($_GET['type']) && $_GET['type'] != ''){
        switch($_GET['type']){
            case 'css':
                header('Content-Type: text/css');
                break;
    
            case 'js':
                header('Content-Type: text/javascript');
                break;
    
            case 'json':
                header('Content-Type: application/json');
                break;
    
            case 'rss':
                header('Content-Type: application/rss+xml; charset=ISO-8859-1');
                break;
    
            case 'xml':
                header('Content-Type: text/xml');
                break;
    
            default:
                header('Content-Type: text/plain');
                break;
        }
    
    # Otherwise, try and determine what file type should be output by the file extension from the link
    }else{
        # See if we can find a file type in the link specified and set the headers accordingly
    
        # If css file extension is found, then set the headers to css format
        if(strstr($_GET['link'], '.css') != FALSE){
            header('Content-Type: text/css');
    
        # If javascript file extension is found, then set the headers to javascript format
        }elseif(strstr($_GET['link'], '.js') != FALSE){
            header('Content-Type: text/javascript');
    
        # If json file extension is found, then set the headers to json format
        }elseif(strstr($_GET['link'], '.json') != FALSE){
            header('Content-Type: application/json');
    
        # If rss file extension is found, then set the headers to rss format
        }elseif(strstr($_GET['link'], '.rss') != FALSE){
            header('Content-Type: application/rss+xml; charset=ISO-8859-1');
    
        # If css xml extension is found, then set the headers to xml format
        }elseif(strstr($_GET['link'], '.xml') != FALSE){
            header('Content-Type: text/xml');
    
        # If we still haven't found a suitable file extension, then just set the headers to plain text format
        }else{
            header('Content-Type: text/plain');
        }
    }
    
    # Now get the contents of our page we're wanting
    $contents = file_get_contents($_GET['link']);
    
    # And finally, spit everything out
    echo $contents;
    ?>
    
    0 讨论(0)
  • 2020-11-30 20:32

    The great service RawGit was already mentioned, but I'll throw another into the ring: GitCDN.link

    Benefits:

    • Lets you link to specific commits, as well as auto-get the latest (aka master)
    • Incurs no damage from high traffic volumes; RawGit asks that it's dev.rawgit.com links be only used during development, where as GitCDN give you access to the latest version, without the danger of the servers exploding
    • Give you the option of auto minifying your HTML, CSS and JavaScript, or serving it as written (https://min.gitcdn.link).
    • Adds compression (GZip)
    • Adds all the correct headers (Content-Type, cache-control, e-tag, etc)

    Full disclosure, I'm a project maintainer at GitCDN.link

    0 讨论(0)
提交回复
热议问题