Class: FileManager

Inherits:
Zarchitect show all
Defined in:
lib/zarchitect/file_manager.rb

Constant Summary

Constants inherited from Zarchitect

Zarchitect::ASSETDIR, Zarchitect::ASSETSDIR, Zarchitect::BUILDIR, Zarchitect::CONFIGDIR, Zarchitect::DEBUGSDIR, Zarchitect::DRAFTDIR, Zarchitect::FILEDIR, Zarchitect::FILESDIR, Zarchitect::HTMLDIR, Zarchitect::LAYOUTDIR, Zarchitect::NODEDIR, Zarchitect::SHARESDIR, Zarchitect::VERSION

Instance Method Summary collapse

Methods inherited from Zarchitect

#main, #rss

Constructor Details

#initializeFileManager

Returns a new instance of FileManager.



3
4
5
6
7
8
9
10
11
# File 'lib/zarchitect/file_manager.rb', line 3

def initialize
  @from = FILEDIR # where user puts files
  @to = File.join(HTMLDIR, FILESDIR) # files in website dir tree

  @img = Array.new
  @audio = Array.new
  @video = Array.new
  @misc  = Array.new
end

Instance Method Details

#cleanObject



81
82
83
84
# File 'lib/zarchitect/file_manager.rb', line 81

def clean
  # remove all files in _html/files
  %x{rm -r _html/files/*} 
end

#copy(from, to) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/zarchitect/file_manager.rb', line 72

def copy(from, to)
  cmd = "cp #{from} #{to}"
  GPI.print cmd, GPI::CLU.check_option('v')
  unless system(cmd)
    GPI.print "failed to copy '#{from}' to '#{to}'"
    GPI.quit
  end
end

#runObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/zarchitect/file_manager.rb', line 13

def run
  # iterate FROM 
  Dir.glob(File.join(@from, '**', '*'), File::FNM_DOTMATCH).reject do |fullpath|
    path = fullpath[(@from.length)..-1]
    next if path[-1] == '.'
    realpath = File.join(@to, path) # path of new dir/symlink
    
    # dir handling / create copies in TO
    Util.mkdir(realpath) if File.directory?(fullpath)
    next if File.directory?(fullpath)
    # file handling
    rrealpath = File.join(Dir.getwd, fullpath)
    # handle file types embedded in posts
    if Image.is_valid?(fullpath)
      GPI.print "processing #{fullpath} as image file",
        GPI::CLU.check_option('v')
      @img.push ImageSet.new(path, fullpath, realpath)
    elsif Audio.is_valid?(fullpath)
      GPI.print "processing #{fullpath} as audio file",
        GPI::CLU.check_option('v')
      @audio.push Audio.new(fullpath)
    elsif Video.is_valid?(fullpath)
      GPI.print "processing #{fullpath} as video file",
        GPI::CLU.check_option('v')
      @video.push Video.new(fullpath)
    else
      GPI.print "processing #{fullpath} as any file",
        GPI::CLU.check_option('v')
      @misc.push MiscFile.new(fullpath)
    end
    # create symlink in _html/files to physical files _files (if process did
    # not abort)
    unless File.exist?(realpath)
      if Zarchitect.conf.symlink_files
        symlink(rrealpath, realpath)
      else
        copy(rrealpath, realpath)
      end
    else
      if File.stat(rrealpath).mtime > File.stat(realpath).mtime
        if Zarchitect.conf.symlink_files
          symlink(rrealpath, realpath)
        else
          copy(rrealpath, realpath)
        end
      end
    end

  end
end


64
65
66
67
68
69
70
# File 'lib/zarchitect/file_manager.rb', line 64

def symlink(from, to)
  GPI.print "creating symlink #{to} ~> #{from}", 
    GPI::CLU.check_option('v')
  File.symlink(from, to)
  GPI.print "created symlink #{to} ~> #{from}",
    GPI::CLU.check_option('v')
end