Rails 4: Issue Sending .txt files from local machine to Windows Share - ruby-on-rails

I'm currently creating .csv files from a SQL view and writing to
#{Rails.root}/public/
which works no problem. In addition, I need to write these generated files to a Windows share in the form of:
\\NAME-APP.enterprise.company.com\Files
I've tried Net::SCP.upload, Net::SFTP.start, FileUtils, rsync, and even Dir.entries('share url here)` just to see if I can see anything in the folder, which generally results in
No such file or directory # dir_initialize
I can map my local computer to the Windows share point, in the form of:
smb://NAME-APP.enterprise.company.com/Files
but manually dragging and dropping to there isn't an acceptable solution in this case.
Feel like I've hit a wall and may be overlooking something. Have stumbled across this post but to no avail: How do I address a UNC path in Ruby on Windows?
Any advice on this is greatly appreciated.
Edit:
FileUtils.cp_r('/Volumes/Macintosh HD/Users/davidpardy/development/ror/sbb/oct31week/1a/FST-Export/public/1538791_new.txt', '\\\\NAME-APP\\Files')
doesn't return an error, but doesn't upload the .txt file to Files.

The solution is not to use FileUtils.cp_r(source_file, 'smb://...') because smb://... only represents the server address, not the mount folder on your filesystem.
In the terminal, run the mount command to find the path of the mount folder, which is what you'll use in ruby, e.g., FileUtils.cp_r(source_file, '/Volumes/mount_folder_here...').

Related

JSON Error: Files or directories outside directory

I tried to submit an Edge extension. It is a simple extension that had three files: the manifest.json, background.js, and an icon.
When I sideload it in Edge, it works. When I tried to publish it, I got an error that says, Files or directories outside directory.
I have looked everywhere for documentation on what the folder directory should look like. Can someone point me to an example directory? Should there be more folders?
The solution was to create a zip file in another way. I found an online site, but if this was something important, I would find another computer.
Anyway, it seems that Big Sur adds something extra that MS doesn't like. Strange Google accepts it.

How to move Active Storage data from one machine to another

Because reasons we are trying to move a system from one machine to another one. It has several files in the storage directory. I rsynced it (using -a) to a local environment to see if everything works, but turns out not all the files are available, some of them raise an exception:
Errno::ENOENT (No such file or directory # rb_file_s_mtime - /path/to/project/storage/as/df/asdfasdfasdfasdfasdf):
Of course I checked the routes and they exists. I've been reading a bit about how Active Storage works and I maybe the URLs are getting invalidated for some reason, but why some files work? 🧐 Why the exception mentions mtime? And more importantly, how can I do the migration smoothly?
Thanks in advance
So the problem is actually the filesystems + Active Record names 😰 You can consider this a corner case: My local machine runs macOS, while the server runs Linux, so if I had folders Vf and VF on Linux, on macOS they become one (whichever is downloaded firs). Active Storage relies on case-sensitive filenames, and that's why some of the files work fine, but others are not found

Rails on Multiple Hard Drives?

Is there a way to configure Rails to work across multiple hard drives? I have Ruby and Rails installed on my F: drive, but my project stored on my G: drive. So when I try to open up the site I'm developing, I get the following error:
different prefix: "F:/" and "G:/"
about one of the stylesheets. Is there any way to fix this, or do I need to store everything on the same drive?
If you are working on Windows 7 or Vista, you can use the mklink command to create a symbolic link from one directory to another, even if they are on different hard drives.

UNC build path in Team build (TFS)

I am pretty new to TFS so I have this problem when I want to set a destination directory to my build.
Every time I launch the build, it dramatically fails and says "couldn't find \xxxxxxx\yyyyy\zzzz".
I think that my UNC path is wrong.
Let's assume that my drop directory (destination directory) is "C:\Project\" and my computer name is "lemoos-PC", what would the equivalent of my path in UNC notation?
I've tried these :
\\lemoos-PC\C\Project\
\\lemoos-PC\C:\Project\
\\lemoos-PC\Project\
but they don't seem to work.
Thanks in advance .
In your case, you should be able to use the following as your UNC drop location:
\\lemoos-PC\C$\Project
C$ is a special 'administrative' share that is normally already available on your machine. The $ makes it a hidden share that prevents other people from browsing to \\lemoos-PC and seeing it listed. Other people can only access it if they both a) know the name of the share and b) have administrative access to the computer.
The UNC path is really a path to a shared network folder and it would have the format
\\computername\sharename\directory\in\share
so it would depend on what the name of your network share is, not necessarily what your local path is. If you for example share your C:\Project folder as Project, the path would be
\\lemoos-PC\Project\

Finding unused images in a Rails app?

I'm familiar with tools like Deadweight for finding CSS not in use in your Rails app, but does anything exist for images? I'm sitting in a project with a massive directory of assets from working with a variety of designers and I'm trying to trim the fat in this project. It's especially a pain when moving assets to our CDN.
Any thoughts?
It depends greatly on the code using the images. It's always possible that a filename is computed (by concatenating two values or string substitution etc) so a simply grepping by filename isn't necessarily enough.
You could try running wget (probably already installed if you've got a linux machine, otherwise http://users.ugent.be/~bpuype/wget/ ) to mirror your whole site. Do this on the same machine or network if you can, it'll crawl your whole site and grab all the images
# mirror mysite.com accepting only jpg, png and gif files
wget -A jpg,png,gif --mirror www.mysite.com
Once you've done that, you're going to have a second copy of your site's hierarchy containing any images that are actively linked to by any page reachable by crawling your site. You can then backup your source image directory, and replace it with wget's copy. Next, monitor your log files for 404's pertaining to gif/jpg/png files. Hope that helps.
Finding unsed images should be easier than CSS.
Just find *.jpg *.png *gif with glob, put those filenames to dictionary or array and find those filenames againt html, css, js files, remove filename if found and you will get unused list, and move those images to another folder with same directory structure (It will be good for restoring for just in case)
Basically like this, and of course for the file names that encrypted/encoded/obcuscated will not work.
require "fileutils"
img=Dir.glob("**/*.jpg")+Dir.glob("**/*.png")+Dir.glob("**/*.gif")
data=Dir.glob("**/*.htm*")+Dir.glob("**/*.css")+Dir.glob("**/*.js")
puts img.length.to_s+" images found & "+data.length.to_s+" files found to search against"
content=""
data.each do |f|
content+=File.open(f, 'r').read
end
img.each do |m|
if not content=~ Regexp.new("\\b"+File.basename(m)+"\\b")
FileUtils.mkdir_p "../unused/"+File.dirname(m)
FileUtils.mv m,"../unused/"+m
puts "Image "+m+" moved to ../unused/"+File.dirname(m)+" folder"
end
end
PS: I used fileutils, because normal makedirs and mv are not works in my windows version of ruby
And I am not good at ruby, so please double check it before you use it.
Here is the sample results I ran in root folder of sample rails folder in my windows
---\ruby>ruby img_coverage.rb
5 images found & 12 files found to search against
Image depot/public/images/test.jpg moved to ../unused/depot/public/images folder
If your image URLs often come from many computed / concatenated strings and other stuff hard to track programmatically within your source code, and your application is in heavy use, you could try a soft "honeypot" approach like this:
Move all the assets to a different directory, e.g. /attic
Set up an empty /images directory (or what your asset directory is called)
Set up a .htaccess file (if you're on Apache of course) that, using the -f flag, redirects all requests to nonexistent image files to a script
The script copies the requested file from the /attic into the /images directory and displays it
The next request to that image will go directly to the image, because it exists now
After some time and sufficient usage, all needed images should have been copied to the assets directory.
It's a "soft" approach of course because a dialog / situation could have not been opened/entered/used by any user during that time (things like error message icons for example). But it will recognize all used files, no matter where they're requested from, and might help sort out much of the unneeded files.
If your file manager supports it, try sorting your images directory by the files' "last accessed" date. Files that haven't been accessed in a long time most likely aren't used any longer.
Along the same lines, you can also filter or grep through your web server's logs and make a list of the image files that it has served up in the last several months. Any images not in this list are likely unused.

Resources