Sha256: 3faa6fa04e1eb46c5ddc59c490c9b391e5ebfcb87ac408ebd1c6e0551a045170

Contents?: true

Size: 1.42 KB

Versions: 50

Compression:

Stored size: 1.42 KB

Contents

require 'sinatra'
require 'sinatra/cross_origin'
require 'fileutils'

require_relative '../../lib/config/runlist'
require_relative '../../lib/config/config'
require_relative '../../nutella_lib/framework_core'


# Set Sinatra to run in production mode
set :environment, :production

# Set Sinatra's port to nutella's main_interface_port
pt = Nutella.config['main_interface_port'] + 2
set :port, pt

# Enable Sinatra to process CORS requests
set :allow_origin, :any
set :allow_methods, [:get, :post, :options]

configure do
  enable :cross_origin
end



# Set data folder
data_folder = "#{ENV['HOME']}/.nutella/data/binary-files-manager"

# If data folder doesn't exist, create it
unless Dir.exists? data_folder
  FileUtils::mkdir_p data_folder
end



# Serve all files in the data folder
get "/:filename" do |fn|
  send_file "#{data_folder}/#{fn}"
end


# Handle file upload
post "/upload" do
  file_name = params[:filename]
  file_path = "#{data_folder}/#{file_name}"

  # If the file already exists, just reply with the file url
  return {url: url(file_path)}.to_json if File.exist? file_path

  # Otherwise, write the file and reply with URL
  File.open(file_path, 'w') do |f|
    f.write(params['file'][:tempfile].read)
  end
  {url: url(file_name)}.to_json
end


# Tests if a particular file exists
get "/test/:filename" do |fn|
  if File.exist? "#{data_folder}/#{fn}"
    {url: url("/#{fn}")}.to_json
  else
    {error: 404}.to_json
  end
end

Version data entries

50 entries across 50 versions & 1 rubygems

Version Path
nutella_framework-0.9.2 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.9.1 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.9.0 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.8.0 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.7.3 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.7.2 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.7.1 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.7.0 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.21 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.20 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.19 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.18 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.17 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.16 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.15 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.13 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.12 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.11 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.10 framework_components/binary-files-manager/bin_files_mngr.rb
nutella_framework-0.6.9 framework_components/binary-files-manager/bin_files_mngr.rb