<commit_before>require "sys/filesystem"
#require "rack/boxlet_url_builder"
require "rack/request"
require "rack/response"
require "rack/file_upload"
require "boxlet/db"
require "boxlet/app/router"


module Boxlet
  class App

    include Sys

    def self.routes
      routes = {
        # ["/", :get]                 => :index,
        # ["/auth"]                   => :auth,
        # ["/register_device", :post] => :register_device,
        # ["/notifications", :*]   => :notifications,
        ["/stats", :post]           => :stats,
        ["/push_files", :post]      => :push_files,
        ["/file_list"]              => :file_list,
        ["/file_info"]              => :file_info,
        ["/resync", :get]           => :resync
      }
    end

    def initialize
      if Boxlet.debug?
        usage = Boxlet::App.app_space_usage
        capacity = Boxlet::App.app_space_capacity
        puts "Space Utilization: #{usage}MB / #{capacity}MB (#{(usage.to_f / capacity).round(3)}%)"
      end
    end

    def bind
      Rack::Builder.new do
        use Rack::Reloader
        use Rack::FileUpload, :upload_dir => [Boxlet.config[:upload_dir] || APP_ROOT + "/uploads"]
        use Rack::Static, :urls => ["/uploads"]

        Boxlet::App.routes.each do |route, action|
          map route[0] do
            run Boxlet::Router.new(route[1] || :*, action)
          end
        end
      end.to_app
    end


    def setup(config)
      begin
        # Check for free space
        if Boxlet::App.free_space <= 50
          raise "Not enough free space"
        end
        if Boxlet::App.app_space_usage / Boxlet::App.app_space_capacity >= 0.9
          puts "App is over 90% full"
        end

        # Create upload and tmp directories
        upload_dir = Boxlet.config[:upload_dir]
        tmp_dir = Boxlet.config[:tmp_dir]
        if !File.exists?(upload_dir) || !File.exists?(tmp_dir)
          if !File.exists?(upload_dir)
            puts "Upload directory (#{upload_dir}) does not exist.  Create? [y/n]"
            create = gets.chomp
            if create =~ /y/i
              Dir.mkdir(upload_dir)
              puts "Upload directory created!"
            end
          end
          if !File.exists?(tmp_dir)
            puts "Temp directory (#{tmp_dir}) does not exist.  Create? [y/n]"
            create = gets.chomp
            if create =~ /y/i
              Dir.mkdir(tmp_dir)
              puts "Temp directory created!"
            end
          end
          if File.exists?(upload_dir) && File.exists?(tmp_dir)
            puts "Done creating directories."
          else
            raise "Error creating directories.  Please check your config and file permissions, and retry."
          end<commit_msg>updaiting setup command<commit_after>require "sys/filesystem"
#require "rack/boxlet_url_builder"
require "rack/request"
require "rack/response"
require "rack/file_upload"
require "boxlet/db"
require "boxlet/app/router"


module Boxlet
  class App

    include Sys

    def self.routes
      routes = {
        # ["/", :get]                 => :index,
        # ["/auth"]                   => :auth,
        # ["/register_device", :post] => :register_device,
        # ["/notifications", :*]   => :notifications,
        ["/stats", :post]           => :stats,
        ["/push_files", :post]      => :push_files,
        ["/file_list"]              => :file_list,
        ["/file_info"]              => :file_info,
        ["/resync", :get]           => :resync
      }
    end

    def bind
      if Boxlet.debug?
        usage = Boxlet::App.app_space_usage
        capacity = Boxlet::App.app_space_capacity
        puts "Space Utilization: #{usage}MB / #{capacity}MB (#{(usage.to_f / capacity).round(3)}%)"
      end

      Rack::Builder.new do
        use Rack::Reloader
        use Rack::FileUpload, :upload_dir => [Boxlet.config[:upload_dir] || APP_ROOT + "/uploads"]
        use Rack::Static, :urls => ["/uploads"]

        Boxlet::App.routes.each do |route, action|
          map route[0] do
            run Boxlet::Router.new(route[1] || :*, action)
          end
        end
      end.to_app
    end


    def setup(config)
      begin
        # Create upload and tmp directories
        upload_dir = Boxlet.config[:upload_dir]
        tmp_dir = Boxlet.config[:tmp_dir]
        if !File.exists?(upload_dir) || !File.exists?(tmp_dir)
          if !File.exists?(upload_dir)
            puts "Upload directory (#{upload_dir}) does not exist.  Creating..."
            Dir.mkdir(upload_dir)
            puts "Upload directory created!"
          end
          if !File.exists?(tmp_dir)
            puts "Temp directory (#{tmp_dir}) does not exist.  Creating..."
            Dir.mkdir(tmp_dir)
            puts "Temp directory created!"
          end
          if File.exists?(upload_dir) && File.exists?(tmp_dir)
            puts "Done creating directories."
          else
            raise "Error creating directories.  Please check your config and file permissions, and retry."
          end
        end

        # Check for free space
        if Boxlet::App.free_space <= 50
          raise "Not enough free space"
        end
        if Boxlet::App.app_space_usage / Boxlet::App.app_space_capacity >= 0.9
          puts "App is over 90% full"
{'fraction_non_alphanumeric': 0.12526017029328287, 'fraction_numerical': 0.0034058656575212867, 'mean_word_length': 1.844994617868676, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, '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': '870068', 'n_tokens_mistral': 1605, 'n_tokens_neox': 1575, 'n_words': 384}