<commit_before>require 'time'
require 'sinatra'
require 'sinatra/flash'
require './lib/jcifs'
require 'uri'

enable :sessions
set :session_secret, '<KEY>'
set :views, File.dirname(__FILE__) + '/../views'
set :public, File.dirname(__FILE__) + '/../public'

get '/' do
    @session = session
    haml :index
end

get '/get' do
    file = params[:file]
    redirect to('/get/' + URI.escape(file, /[^A-Za-z0-9\/]/))
end

get '/get/*' do
    file = params[:splat][0]
    session[:file] = file

    begin
        if session[:pass] and !session[:pass].empty?
            @smbfile = CIFS::File.get(file, session[:domain], session[:user], session[:pass])
        else
            @smbfile = CIFS::File.get(file)
        end

        if ! @smbfile.exists
            raise "File does not exist"
        end

        if @smbfile.isDirectory
            @dir = CIFS::DirReader.new(@smbfile)
            return haml :directory
        end

        if @smbfile.isFile
            fr = CIFS::FileReader.new(@smbfile)
            headers \
                'Last-Modified' => Time.at(@smbfile.getLastModified / 1000).httpdate,
                'Content-Length' => @smbfile.length.to_s,
                'Content-Type' => fr.mime_type.to_s,
                'Content-Disposition' => 'filename="%s"' % @smbfile.getName
            return fr
        end
    rescue CIFS::SmbAuthException => e
        flash[:error] = e.message
        flash[:message] = "please supply valid credentials"
        return redirect to('/')
    rescue Exception => e
        flash[:error] = e.message
        return redirect to('/')
    end

end

post '/credentials/set' do
    [:user, :pass, :domain].each do |i|
        session[i] = params[i]
    end
    redirect to('/')
end

post '/credentials/remove' do
    [:user, :pass, :domain].each { |i| session.delete(i) }
    redirect to('/')
end<commit_msg>retry on trailing slash exception<commit_after>require 'time'
require 'sinatra'
require 'sinatra/flash'
require './lib/jcifs'
require 'uri'

enable :sessions
set :session_secret, '<KEY>'
set :views, File.dirname(__FILE__) + '/../views'
set :public, File.dirname(__FILE__) + '/../public'

get '/' do
    @session = session
    haml :index
end

get '/get' do
    file = params[:file]
    redirect to('/get/' + URI.escape(file, /[^A-Za-z0-9\/]/))
end

get '/get/*' do
    file = params[:splat][0]
    session[:file] = file

    begin
        if session[:pass] and !session[:pass].empty?
            @smbfile = CIFS::File.get(file, session[:domain], session[:user], session[:pass])
        else
            @smbfile = CIFS::File.get(file)
        end

        if ! @smbfile.exists
            raise "File does not exist"
        end

        if @smbfile.isDirectory
            @dir = CIFS::DirReader.new(@smbfile)
            return haml :directory
        end

        if @smbfile.isFile
            fr = CIFS::FileReader.new(@smbfile)
            headers \
                'Last-Modified' => Time.at(@smbfile.getLastModified / 1000).httpdate,
                'Content-Length' => @smbfile.length.to_s,
                'Content-Type' => fr.mime_type.to_s,
                'Content-Disposition' => 'filename="%s"' % @smbfile.getName
            return fr
        end
    rescue CIFS::SmbAuthException => e
        flash[:error] = e.message
        flash[:message] = "please supply valid credentials"
        return redirect to('/')
    rescue Exception => e
        if e.message.end_with?("must end with '/'") and ! file.end_with?('/')
            file += '/'
            session[:file] = file
            retry
        end
        flash[:error] = e.message
        return redirect to('/')
    end

end

post '/credentials/set' do
    [:user, :pass, :domain].each do |i|
        session[i] = params[i]
    end
    redirect to('/')
end

post '/credentials/remove' do
    [:user, :pass, :domain].each { |i| session.delete(i) }
    redirect to('/')
end
{'fraction_non_alphanumeric': 0.15138319672131148, 'fraction_numerical': 0.003586065573770492, 'mean_word_length': 2.131515637530072, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1109617', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1240, 'n_words': 300}