Getting "Unknown file type" in ruby

901 views Asked by At

here's my code:

> !#usr/bin/ruby

require 'fileutils'

Dir.chdir "/home/john/Documents"

if (Dir.exist?("Photoshoot") === false) then
    Dir.mkdir "Photoshoot"
    puts "Directory: 'Photoshoot' created"
end

Dir.chdir "/run/user/1000/gvfs"

camdirs = Dir.glob('*')
numcams = camdirs.length
camnum = 0
campath = []

while camnum < numcams do
    campath.push("/run/user/1000/gvfs/#{camdirs[camnum]}/DCIM")
    puts campath[camnum]

    camnum += 1
end

campath.each do |path|
    Dir.chdir (path)
    foldnum = 0
    foldir = Dir.glob('*')
    puts foldir
    Dir.entries("#{path}/#{foldir[foldnum]}").each do |filename|
        filetype = File.extname(filename)
        if filetype == ".JPG"
            FileUtils.mv("#{path}/#{foldir[foldnum]}/#{filename}", "/home/john/Documents/Photoshoot")
        end
        foldnum += 1
    end


end
puts "#{numcams} cameras detected"

I'm just trying to go into some cameras I have connected and extract all the images into a file but its giving me this error. One of the things that's messing me up is that the images are stored in sub-folders under DCIM. When I just use .entries it gives me the folders the images are in as well as the images.

/usr/lib/ruby/2.3.0/fileutils.rb:1387:in `copy': unknown file type: /run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C021%5D/DCIM//IMG_0092.JPG (RuntimeError)
    from /usr/lib/ruby/2.3.0/fileutils.rb:472:in `block in copy_entry'
    from /usr/lib/ruby/2.3.0/fileutils.rb:1498:in `wrap_traverse'
    from /usr/lib/ruby/2.3.0/fileutils.rb:469:in `copy_entry'
    from /usr/lib/ruby/2.3.0/fileutils.rb:530:in `rescue in block in mv'
    from /usr/lib/ruby/2.3.0/fileutils.rb:527:in `block in mv'
    from /usr/lib/ruby/2.3.0/fileutils.rb:1571:in `block in fu_each_src_dest'
    from /usr/lib/ruby/2.3.0/fileutils.rb:1585:in `fu_each_src_dest0'
    from /usr/lib/ruby/2.3.0/fileutils.rb:1569:in `fu_each_src_dest'
    from /usr/lib/ruby/2.3.0/fileutils.rb:517:in `mv'
    from /home/john/Desktop/TestExtract.rb:34:in `block (2 levels) in <main>'
    from /home/john/Desktop/TestExtract.rb:31:in `each'
    from /home/john/Desktop/TestExtract.rb:31:in `block in <main>'
    from /home/john/Desktop/TestExtract.rb:26:in `each'
    from /home/john/Desktop/TestExtract.rb:26:in `<main>'
/run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C022%5D/DCIM
/run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C021%5D/DCIM
/run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C020%5D/DCIM
104___03
105___04
106___05
102___01
[Finished in 0.1s with exit code 1]
[shell_cmd: ruby "/home/john/Desktop/TestExtract.rb"]
[dir: /home/john/Desktop]
[path: /home/john/bin:/home/john/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]

Any advice? I can't figure out what's wrong.

1

There are 1 answers

2
anothermh On

The reason the path to your files looks strange is because your camera storage has been mounted using FUSE. If you look very closely, you'll see that it is looking for:

/run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C021%5D/DCIM//IMG_0092.JPG

You have two forward slashes before the final filename. Try correcting this on line 34 of your app.

If the problem still manifests then it is possible that the user running the operation in Ruby does not have permission to that filesystem or the manner in which the paths are constructed by FUSE is not compatible with Ruby FileUtils.

You can try to run:

cat /run/user/1000/gvfs/gphoto2:host=%5Busb%3A002%2C021%5D/DCIM/IMG_0092.JPG

as the same user that is running the Ruby process to ensure you have read permission to the filesystem.