<commit_before>require "pathname"
require "securerandom"
require "slim"

require_relative "base"
require_relative "../input"
require_relative "../helpers"
require_relative "../directory"
require_relative "../form_helpers"

module Tint
	module Controllers
		class File < Base
			helpers Tint::Helpers::Rendering

			def self.query(val)
				condition { request.query_string == val }
			end

			def self.params(key)
				condition { !!params[key] }
			end

			def self.if(block)
				condition(&block)
			end

			namespace "/:site/files" do
				get "/?*", query: "download" do
					if resource.exist? && resource.file?
						authorize resource, :show?
						send_file resource.path, filename: resource.name, type: resource.mime, disposition: :attachment
					else
						slim :error, locals: { message: "Only files may be downloaded." }
					end
				end

				get "/?*", query: "source" do
					authorize resource, :edit?

					if resource.text?
						html = slim :"layouts/files" do
							slim :"files/source", locals: { path: resource.route }
						end

						stream_into_element("<textarea name=\"source\">", html, resource)
					else
						slim :error, locals: { message: "Only text files may be edited by source" }
					end
				end

				get "/?*", if: -> { resource.directory? || !resource.exist? } do
					authorize resource, :index?

					slim :"layouts/files", locals: { directory: resource } do
						slim :"files/index", locals: { directory: resource }
					end
				end

				get "/?*", if: -> { resource.yml? || !resource.content? } do
					authorize resource, :edit?

					slim :"layouts/files" do
						slim :"files/yml", locals: {
							data: resource.frontmatter,
							path: resource.route
						}
					end
				end

				get "/?*", if: -> { resource.text? } do
					authorize resource, :edit?

					frontmatter = resource.frontmatter? && resource.frontmatter
					html = slim :"layouts/files" do
						slim :"files/text", locals: {
							frontmatter: frontmatter,
							wysiwyg: resource.markdown?,
							path: resource.route
						}
					end

					stream_into_element("<textarea name=\"content\">", html, resource)
				end

				get "/?*" do
					authorize resource, :edit?
					slim :"layouts/files" do
						slim :"files/binary", locals: {
							file: resource,
							input: Input::File.new(:file, "file", resource.relative_path, site)
						}
					end
				end

				put "/*", params: :name do
					authorize resource, :update?

					new = resource.parent.resource(params[:name])
					if new.exist?
						slim :error, locals: { message: "A file with that name already exists" }
					else
						site.commit_with("Renamed #{resource.relative_path} to #{new.name}", pundit_user) do |dir|
							dir.join(resource.relative_path).rename(dir.join(new.relative_path))
						end<commit_msg>Allow us to not repeat the layout declaration<commit_after>require "pathname"
require "securerandom"
require "slim"

require_relative "base"
require_relative "../input"
require_relative "../helpers"
require_relative "../directory"
require_relative "../form_helpers"

module Tint
	module Controllers
		class File < Base
			helpers Tint::Helpers::Rendering

			def self.query(val)
				condition { request.query_string == val }
			end

			def self.params(key)
				condition { !!params[key] }
			end

			def self.if(block)
				condition(&block)
			end

			def slim(template, options)
				return super(template, options) if template == :error

				super :"layouts/files" do
					super :"files/#{template}", options
				end
			end

			namespace "/:site/files" do
				get "/?*", query: "download" do
					if resource.exist? && resource.file?
						authorize resource, :show?
						send_file resource.path, filename: resource.name, type: resource.mime, disposition: :attachment
					else
						slim :error, locals: { message: "Only files may be downloaded." }
					end
				end

				get "/?*", query: "source" do
					authorize resource, :edit?

					if resource.text?
						slim :source, locals: { path: resource.route }
						stream_into_element("<textarea name=\"source\">", html, resource)
					else
						slim :error, locals: { message: "Only text files may be edited by source" }
					end
				end

				get "/?*", if: -> { resource.directory? || !resource.exist? } do
					authorize resource, :index?
					slim :index, locals: { directory: resource }
				end

				get "/?*", if: -> { resource.yml? || !resource.content? } do
					authorize resource, :edit?

					slim :yml, locals: { data: resource.frontmatter, path: resource.route }
				end

				get "/?*", if: -> { resource.text? } do
					authorize resource, :edit?

					frontmatter = resource.frontmatter? && resource.frontmatter
					slim :text, locals: {
						frontmatter: frontmatter,
						wysiwyg: resource.markdown?,
						path: resource.route
					}

					stream_into_element("<textarea name=\"content\">", html, resource)
				end

				get "/?*" do
					authorize resource, :edit?
					slim :binary, locals: {
						file: resource,
						input: Input::File.new(:file, "file", resource.relative_path, site)
					}
				end

				put "/*", params: :name do
					authorize resource, :update?

					new = resource.parent.resource(params[:name])
					if new.exist?
						slim :error, locals: { message: "A file with that name already exists" }
					else
						site.commit_with("Renamed #{resource.relative_path} to #{new.name}", pundit_user) do |dir|
							dir.join(resource.relative_path).rename(dir.join(new.relative_path))
						end
{'fraction_non_alphanumeric': 0.1324949881538181, 'fraction_numerical': 0.0, 'mean_word_length': 2.8217270194986073, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1644580', 'n_tokens_mistral': 2266, 'n_tokens_neox': 1664, 'n_words': 464}