(request, path, obj, download=False)
return data
Authenticates access through decorator before serving file.
Authenticates access through decorator before serving file.
Authenticates access through decorator before serving file .
def file_serve(request, path, obj, download=False):
    """
    Authenticates access through decorator before serving file.
    """

    # Get the object that corresponds to the entry.
    root = obj.get_data_dir()

    # This will turn into an absolute path.
    file_path = join(root, path)

    # Ensure only files in the object root can be accessed.
    if not file_path.startswith(root) or not os.path.isfile(file_path):
        msg = "Invalid path." if (not file_path.startswith(root)) else f"File not found: {path}"
        messages.error(request, msg)
        return redirect(obj.url())

    # The response will be the file content.
    mimetype = auth.guess_mimetype(fname=path)

    # Get the filesize in Mb
    size = os.path.getsize(file_path) / 1024 / 1024

    # This behavior can be further customized in front end webserver.
    if size < 20 and not download:
        # Return small files in the browser if possible.
        data = sendfile(request, file_path, mimetype=mimetype)
    else:
        # Trigger a file download for bigger files.
        fname = os.path.basename(file_path)
        data = sendfile(request, file_path, attachment=True, attachment_filename=fname, mimetype=mimetype)

    return data
def file_serve ( request , path , obj , download = False ) : # Get the object that corresponds to the entry. root = obj . get_data_dir ( ) # This will turn into an absolute path. file_path = join ( root , path ) # Ensure only files in the object root can be accessed. if not file_path . startswith ( root ) or not os . path . isfile ( file_path ) : msg = "Invalid path." if ( not file_path . startswith ( root ) ) else f"File not found: {path}" messages . error ( request , msg ) return redirect ( obj . url ( ) ) # The response will be the file content. mimetype = auth . guess_mimetype ( fname = path ) # Get the filesize in Mb size = os . path . getsize ( file_path ) / 1024 / 1024 # This behavior can be further customized in front end webserver. if size < 20 and not download : # Return small files in the browser if possible. data = sendfile ( request , file_path , mimetype = mimetype ) else : # Trigger a file download for bigger files. fname = os . path . basename ( file_path ) data = sendfile ( request , file_path , attachment = True , attachment_filename = fname , mimetype = mimetype ) return data