google-drive-ruby: upload file to a folder duplicates the file in the folder and root path - ruby

I have writen a Ruby script to lookup for documents with a given date and upload them to Google Drive by using the google-drive-ruby gem. I have a folder inside of the gdrive root path where I want to place the files, and I access it using collection_by_title and then uploading the file by using the .add method.
The problem is that the files are being uploaded each one two times, one to the folder I want and another one to the root path of my GDrive. Any thoughts?
This is the method where the file gets uploaded:
def upload_document(file, folder_code)
folder = #session.collection_by_title("#{folder_code}")
path = "#{#basedir}/#{folder_code}/#{file}"
folder.add(#session.upload_from_file(path, file, convert: false))
end
EDIT: Methods and variables translated to english.
Each time the method upload_document is triggered, one copy of the file gets uploaded to the folder and another copy gets uploaded to the root path of gdrive.
Example: Method upload_document gets called providing the file (454327.pdf) and the code of the folder where it has to be uploaded in gdrive ("1"). I build the folder object by using collection_by_title, I build the path where the file is located in my local network, and finally the file gets uploaded using upload_from_file. At this point, two copies of the file had been uploaded, one to the root path of gdrive (which I don't want) and another one to the right folder in gdrive.

I received an answer from the gem creator explaining what is happening and my script is finally working as I expected.
https://github.com/gimite/google-drive-ruby/issues/260
The thing is that the file is firstly uploaded to the root by default and then .add just moves the file to the selected collection, so the file needs to be removed from root after the move operation is completed.
#session.root_collection.remove(file)

Related

How to move jar file from one folder to another in Ruby?

In my Ruby script I have to copy one jar file from lib to a different location. Below is the piece of code where I am trying to do so:
FileUtils.mv("#{$HOME_PATH}/lib/myjar1.jar", "#{$HOME_PATH}")
But it is not able to move the jar file to destination. I am seeing the following error:
No such file or directory - file:/home/path/lib/myjar1.jar or file:/home/path/myjar1.jar
I double checked that myjar1.jar is present in source directory. Not sure why I am seeing that error.
I also tried using FileUtils.cp_r("#{$HOME_PATH}/lib/myjar1.jar", "#{$HOME_PATH}") and I am seeing the following error: unknown file type: file:/home/path/lib/myjar1.jar.
Any guidance in copying or moving a jar file is appreciated.
Im assuming you are starting from your home directory (hence the $HOME_PATH). I prefer to use the given ENV hash in IRB.
In IRB this works in MAC OSX:
>> FileUtils.mv("#{ENV['HOME']}/Desktop/my.jar", "#{ENV['HOME']}")
0
The FileUtils#mv method returns 0 as that is the return code of the Unix version of mv which just means it was successful.

Laravel location of created files

I'm attempting to modify a text file stored on the server using Larvel's File::put(filelocation,filecontents)
but I can't figure out what location this is relative to on the server. If I have a file "somestuff.json" within my LaravelApp/public folder, what location string do I use for a parameter to File::put() ?
File name for File::methods() is not relative you have to give the full file path:
File::put('/var/www/LaravelApp/public/somestuff.json', $filecontents);
But Laravel has some helpers to help you with this:
File::put(public_path().'/somestuff.json', $filecontents);
Also:
base_path(); // the base of your application LaravelApp/
app_path(); // Your LaravelApp/app
They are all in the file vendor/laravel/framework/src/Illuminate/Support/helpers.php.

wsadmin upload file from local machine to remote

I'm trying to automate process of deployment and I want to upload some files to WAS using wsadmin (jython). My question is if it is possible to upload file from my standalone wsadmin to remote WAS Server. And if so, is it possible to upload file somewhere out of application (fe. /opt/IBM/WebSphere/AppServer/temp)? I don't want to upload it to specific profile, but to server root.
When I'm deploying application it is copying war/ear file to WAS, so is it there some mechani to upload separate file?
many thanks
AntAgent allows you to upload any file, provided that the content of the file can fit in memory:
https://www.ibm.com/support/knowledgecenter/en/SSAW57_8.5.5/com.ibm.websphere.javadoc.doc/web/mbeanDocs/AntAgent.html
In wsadmin you'll need to use invoke_jmx method of AdminControl object.
from java.lang import String
import jarray
fileContent = 'hello!'
antAgent = AdminControl.makeObjectName(AdminControl.queryNames('WebSphere:*,type=AntAgent,process=dmgr'))
str = String(fileContent)
bytes = str.getBytes()
AdminControl.invoke_jmx(antAgent, 'putScript', [String('hello.txt'),bytes], jarray.array(['java.lang.String', '[B'], String))
Afterwards you'll find 'hello.txt' file in WAS profile's temp directory. You may use relative paths as well.

Google Drive Root Folder File Not Found Error

I had written a program that creates a file object, which by defaults gets created in the root folder if no parent is specified. Then given the id of the file's parent it retrieves the root folder.
For some strange reason this no longer works, instead of returning the root folder I get a File Not Found error. Any ideas?
Thanks
Update
I tried retrieving the file by passing in "root" as the file_id and I encountered the same error.
Get File By ID Error: File not found: 0AFrTA6Qc9-_TU9PVA
Below is the function used
def get_file_by_id session, file_id
drive = session.discovered_api("drive", "v2")
result = session.execute api_method: drive.files.get, parameters: {'fileId' => file_id}
if result.status == 200
return result.data
else
raise "Get File By ID Error: #{result.data['error']['message']}"
end
end
I am however able to get other file's by ID except the root.
Without seeing code, hard to say what you're doing wrong. But, there's a much easier way to go about getting the root folder for a user than what you're doing!
You can use the special 'root' ID to reference the root folder of the current user.
GET https://www.googleapis.com/drive/v2/files/root
That will return back info about the root folder. It's also available in the about collection.
Update: Details about the root folder are only available using the https://www.googleapis.com/auth/drive scope. When using the file scope, there isn't a way to 'open' the root folder from the Drive UI or Picker in order to authorize access by the app.

Rails 3.1+ and GeoIP database file location/access

This question is related to Rails - Where I have to store data file (.dat) in my rails project - GeoIp City database . I have a rails 3.2 app. I am trying to run:
#geoip = GeoIP.new('GeoLiteCity.dat')
In one of my app's controllers. I unzipped the 'GeoLiteCity.dat' file into the /public folder. I am getting the error "No such file or directory - GeoLiteCity.dat".
I've experimented with putting it in the images assets pipeline folder and some random other places. I continue to get the same error. Not sure how to access this file. Any ideas on what I'm doing wrong or how to access it best with the assets pipeline?
Try referencing it via the full path:
#geoip = GeoIP.new("#{Rails.root}/public/GeoLiteCity.dat")
On a side note, it's probably not a big deal, but I wouldn't put the file in your public directory.

Resources