📜 ⬆️ ⬇️

We write REST application on Sinatra and we fasten Redactor. Part 2

In the first part of the article, we wrote a REST application and configured 1/3 of Redactor.js. Today we will finish our invention by writing the interface for managing the downloaded images, and we will ensure the download of files. When uploading files, we will not use CarrierWave, but go the usual way Ruby.

Let's start by downloading files. I assume that now you have such a file init.rb:
# coding: utf-8 require 'rubygems' require 'sinatra' require 'data_mapper' require 'carrierwave' require 'carrierwave/datamapper' require 'rmagick' require 'json' set :public_directory, './public' class ImageUploader < CarrierWave::Uploader::Base def store_dir 'uploads/images' end def extension_white_list %w(jpg jpeg gif png bmp) end include CarrierWave::RMagick version :thumb do process :resize_to_fill => [100,74] end storage :file end class Post include DataMapper::Resource property :id, Serial property :title, String property :body, Text end class UploadedImages include DataMapper::Resource property :id, Serial property :image, String property :thumb, String mount_uploader :file, ImageUploader end class UploadedFiles include DataMapper::Resource property :id, Serial property :name, String property :path, String end DataMapper.setup(:default, ENV['DATABASE_URL'] || 'sqlite:./db/base.db') DataMapper.finalize DataMapper.auto_upgrade! get '/' do 'REST   Sinatra <a href="/posts">  </a>' end #List posts get '/posts' do @posts = Post.all erb :'index' end #Create new Post get '/posts/new' do erb :'posts/new' end post '/posts/new' do params.delete 'submit' @post = Post.create(params) redirect '/posts' end #Edit post get '/posts/:id/edit' do @post = Post.get(params[:id]) erb :'posts/edit' end #Update post put '/posts/:id/edit' do post = Post.get(params[:id]) post.title = (params[:title]) post.body = (params[:body]) post.save redirect '/posts' end #Delete post get '/posts/:id/delete' do Post.get(params[:id]).destroy redirect '/posts' end post '/upload/image' do params[:file] filename = params[:file][:filename] file = params[:file][:tempfile] upload = UploadedImages.new upload.file = params[:file] upload.image = params[:image] = '/uploads/images/' + File.join(filename) upload.thumb = params[:thumb] = '/uploads/images/thumb_' + File.join(filename) upload.save @images = UploadedImages.all File.open("public/uploads/images/imageslist.json","w") do |f| f.write JSON.pretty_generate(@images) end '<img src="/uploads/images/' + File.join(filename) + '" />' end 

So, create the files folder in the public / uploads directory, write a new model, UploadedFiles, and add the post method to upload files.
 set :files, File.join(settings.public_directory, 'uploads/files') #   public  (set :public_directory, './public'     init.rb)     ,     class UploadedFiles include DataMapper::Resource property :id, Serial # property :name, String #   property :path, String #    end post '/upload/file' do params[:file] filename = params[:file][:filename] file = params[:file][:tempfile] ext = File.extname(filename) #     ,    ,     . if ext == ".doc" || ext == ".zip" || ext == ".dmg" #   3    doc, zip, dmg,        . File.open(File.join(settings.files, filename), 'wb') # -  upload_f = UploadedFiles.new #  UploadedFile    upload_f.name = params[:name] = File.join(filename) #  UploadedFiles  :name,      upload_f.path = params[:path] = '/uploads/files/' + File.join(filename) #  UploadedFiles  :path,       upload_f.save # '<a href="/uploads/files/' + File.join(filename) + '" />' + File.join(filename) + '</a>' #      end end 

Now it remains to tell the editor exactly how to upload files, open layout.erb and find the line

 $('.redactor_1').redactor({toolbar: 'default', lang: 'ru', imageUpload: '/upload/image', imageGetJson: '/uploads/images/imageslist.json'}); 

Add fileUpload setting : '/ upload / file' to look like this
 $('.redactor_1').redactor({toolbar: 'default', lang: 'ru', imageUpload: '/upload/image', fileUpload: '/upload/file', imageGetJson: '/uploads/images/imageslist.json'}); 

We made all the necessary settings: we created a model, set up the path to the downloaded files, actually implemented the file loading itself, made a check for allowed files. This is what I would like to point out, it would be nice to implement an error message if the file extension does not correspond to the allowed one, but there are 2 BUT: first, Redactor.js does everything in the frame and, if you make an error message, it will be written in textarea, which, in my opinion, is not a good idea, if you try to implement an error message via gem sinatra-flash, it is again not an option, since the error will appear only after the page is overloaded. Well, maybe more experienced people will tell in which direction to dig.

Now let's start managing the downloaded images, the maximum that we will do is a list of all downloaded images and their removal.
Add to init.rb
 #init.rb #List UploadedImages get '/images' do @ uploadedimage = UploadedImages.all #         /images (     uploadedimage) erb :'images' # images.erb end #Delete UploadedImage get '/images/:id/delete' do #     ID UploadedImages.get(params[:id]).destroy # ( ,   - ,      !...) redirect '/images' #       end 

Create the images.erb file in the views directory
 <strong><a href="/posts">   post'</a></strong> <h2>  </h2> <% @ uploadedimage.each do |uploadedimages| %> <a href="<%= uploadedimages.image %>"><img src="<%= uploadedimages.thumb %>"></a><br> #         <a href="/images/<%= uploadedimages.id %>/delete"></a><br><br> <% end %> 

Well, we finished our application. By analogy with the management of images can be done and file management. I think it turned out well. And this is not the end, I continue to study Ruby and Sinatra and there will be more articles.
Github source code
PS: Everywhere you come across
 <hh user=posts> 
or
 <hh user=images> 
just replace with @ variable_name

')

Source: https://habr.com/ru/post/144488/


All Articles