Is it possible to read contents of a file uploaded with CarrierWave? - ruby-on-rails

I followed this example, File uploads with CarrierWave to upload an excel file (Classlist) in my application. The file contains the student names, surnames and gender.
When uploading, User will give a name and subject for this "Class". Which will then be stored in MyClasses table. I want to know if I can read the contents of the file and save contents (names,surnames,gender) in the Student table?
The excel file uploaded looks like this...

Related

Identify UIDocument uniquely

I'm working on a document based app with UIDocumentBrowserViewController. The app itself creates txt files which the user can edit and save.
I want to send those files to a companion app on an Apple Watch. The user can then read those on the watch. The UIDocument is read by my app and converted to be sent with WCSession.
The question is now: How can I uniquely identify an UIDocument?
I know I could make my own identifier by putting a UUID in the file name or at the end of each text file, but this is not the sweet solution.
Apple recommends the UUID for a filename approach, but does mention that you can have the user change the display name after it is created.
The UIDocument class assumes a correspondence between the filename of
a document and the document name (also known the display name). By
default, UIDocument stores the filename as the value of the
localizedName property. However, an application should not require a
user to provide the document filename or display name when he or she
creates a new document.
For your application, you should devise some convention for
automatically generating the filenames for your new documents. Some
suggestions are:
Generate a UUID (universally unique identifier) for each document,
optionally with an application-specific prefix.
Generate a timestamp (date and time) for each document, optionally with an application-specific prefix.
Use a sequential numbering system, for example: “Notes 1”, “Notes 2”, and so on.
For the document (display) name, you might initially use the document
filename if that makes sense (such as with “Notes 1”). Or, if the
document contains text and the user enters some text in the document,
you might use the first line (or some part of the first line) as the
display name. Your application can give users some way to customize
the document name after the document has been created.
Why don't you use URLResourceKey.documentIdentifierKey ? This is a persistent identifier which tracks a document across renames and safe-saves.

How to allow users to be able to upload to my google drive in rails app and then provide them a preview link for google doc in the app?

I need my rails app users to be able to upload to my google drive folder. He should click browse file button and after submission, the file should be uploaded to my drive and I should be able to get document preview link and download link for the user in the rails app itself. Please tell me how to proceed with this. I am completely new to rails.
You can use gem paperclip which is Easy file attachment management for ActiveRecord
https://github.com/thoughtbot/paperclip
and then use paperclip-googledrive extension gem to upload file to google drive
https://github.com/evinsou/paperclip-googledrive
You can use the gem google-api-client and use one of its classes DriveService of DriveV3, you can find its documentation here. There you should find how to give access to your drive.
Create a DriveV3::DriveService instance:
require 'google/apis/drive_v3'
drive_service = Google::Apis::DriveV3::DriveService.new
# ... refer to doc to provide authorization ...
you have to save the file in your server and provide that file for GoogleApi to read from and create it. In this method I am creating this local file from a DataURL string and the same file as a source for DriveService:
# 'content' is a dataURL string representing the file's content
# 'folder_id' is the id of the drive's folder to create the file into
file_metadata = {
name: 'my_file_name.pdf',
parents: [folder_id],
description: 'This is my file'
}
Tempfile.open(local_filename) do |f|
f.binmode
f.write(URI::Data.new(content).data)
file = drive_service.createfile(metadata, upload_source: f, fields: 'id')
# file will contain the id that google created for it
end
To get a small image of the file:
file = drive_service.get_file 'your_file_id', fields: 'thumbnailLink'
# => <Google::Apis::DriveV3::File:0x000000 #thumbnail_link="https://lh3.googleusercontent.com/...">
The content of file.thumbnail_link can be inserted into a normal <img src=> tag and displayed.
There is the property webViewLink that will open the file completely in the browser. You can ask for it also in the parameter fields: webViewLink,thumbnailLink,id.
If you want, you can make one single GoogleAPI request to create the file and receive its thumbnailLink by including thumbnailLink into the parameter fields of the function create_file.

Map columns from excel to db in rails engine

I am working on a rails engine which takes a excel file as an input and than saves it to temp folder in app. Than when i click on validate it validates the file and if there are no errors than it will save file to database. I am able to get the columns of excel file but my problem is that a excel file can have different columns than what in my excel file.
Also i am not generating any migration for engine. db table will be provided by the app and my engine will only parse data, validate and than if excel file is valid than it will save all the data in table.
I am getting confused how to set mapping such that it will save data to db.
For e.g. - My engine have model upload.rb where i am doing all validation for excel file and than saving it. Now suppose my app have a model employee.rb which have columns first name, last name, employee id etc. while my excel file can have any alias or different column name for data. e.g. fname, lname etc.
Suppose i have validation in my engine model, so my engine have no idea about mapping, it will only validate and save the data in db. So mapping needs to be set in my app.
Can anyone tell me how can i set mapping so that whenever my engine is mounted it will parse excel and save to to app's db table as per the mapping set ?
Any help will be appreciated.

Create object in Rails from text file

I am building a site with Ruby on Rails where I can display items that are for sale. Currently I can add new items by filling a form with title, price, some text and upload a couple of pictures.
Since there might be many items I would like to have a text file with the data, one item on each line, data separated with comma or whatever. Then I'd like to give this file to my Rail's application that will create the items in the application. Would this be possible?
What you're basically talking about is importing and exporting CSV files.
You can learn about the basic ruby functions available to you when working with CSV files here: http://ruby-doc.org/stdlib-2.3.0/libdoc/csv/rdoc/CSV.html
Here's a video about exporting to csv and here's a video about importing from csv.
You essentially will call :to_csv on some collection, and then use CSV's foreach or some method of iterating the items in the CSV, and you will create your rows from the parsed data.

mvc file upload and database insert

I'm just getting my head wrapped around MVC in .net using VS 2013.
I need a little direction in regards to uploading a file (which is easy) but also inserting data about that image into a database. Specifically I want to allow the end user to enter a description, title etc. about the file being uploaded. On the back-end I want to also add to the meta data a 'Date Created', 'Path to the file', 'Category', and the File Name and a couple other pieces of data that will help with presenting files in the views. I don't want to insert the files in the DB but just use the path in the generated HTML to point to the physical file so the end user can view or download it.
Multiple file types are being used, Audio, Video, Documents, Images.
I can get the file upload to work but writing the controller to accept a file, and end user input, then also add the other fields I need into the database that the user never sees is where I'm stuck. Blending the file upload with the user fields and beack end data is confusing me on how to get all the pieces to work together.
So in short getting File Upload + User Input + non-User Input values all in the same View, Controller, and Model is what I need direction on.
You have to upload your image plus data in a multi-part form.
In your view you will create a POST form that uses "multipart/form-data" encoding, you can then include inputs for model data and your file upload control within the body of the form. When it submits will will create a multi-part form, one part will contain the binary file and another part will contain your data.
On the controller action side you will receive the data with an action akin to
public ActionResult PostFile(MyModel model, HttpPostedFileBase file) {...}
There are numerous posts on SO with more details so I won't go into that.

Resources