-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdiskstation.rb
94 lines (76 loc) · 2.93 KB
/
diskstation.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
require 'net/ssh'
load 'checksum.rb'
load 'sshutils.rb'
class Diskstation
DS1_ROOT = "/volume1/DiskStation/Videos/"
DS2_ROOT = "/volume2/Diskstation 2/Videos/"
MOVIE_ROOT = DS1_ROOT + "Movies/"
TV_ROOT = DS2_ROOT + "TV/"
@host = nil
@user = nil
def initialize(host, user)
@host = host
@user = user
end
def copy(filename, path, fileinfo)
if fileinfo.type == FNParse::EPISODE
copyEpisode(filename, path, fileinfo)
elsif fileinfo.type == FNParse::DATE_EPISODE
copyDateEpisode(filename, path, fileinfo)
elsif fileinfo.type == FNParse::MOVIE
copyMovie(filename, path, fileinfo)
else
false
end
end
def copyEpisode(filename, path, fileinfo)
$log.info("Copying Episode \'#{filename}\'")
remotePath = getEpisodeRemotePath(filename, fileinfo)
$log.info(" -- Copying to #{remotePath}")
copyFile(path, remotePath, filename)
end
def getEpisodeRemotePath(filename, fileinfo)
"#{TV_ROOT}#{fileinfo.title}/Season #{fileinfo.season.to_s}/"
end
def copyDateEpisode(filename, path, fileinfo)
$log.info("Copying Date Episode \'#{filename}\'")
remotePath = getDateEpisodeRemotePath(filename, fileinfo)
$log.info(" -- Copying to #{remotePath}")
copyFile(path, remotePath, filename)
end
def getDateEpisodeRemotePath(filename, fileinfo)
"#{TV_ROOT}#{fileinfo.title}/"
end
def copyMovie(filename, path, fileinfo)
$log.info("Copying Movie \'#{filename}\'")
remotePath = getMoviePath(filename, fileinfo)
$log.info(" -- Copying to #{remotePath}")
copyFile(path, remotePath, filename)
end
def getMoviePath(filename, fileinfo)
"#{MOVIE_ROOT}#{fileinfo.title}/"
end
def copyFile(localPath, remotePath, filename)
ssh = SshUtils.start(@host, @user)
# If the remote path does not exist
if not ssh.pathExists?(remotePath)
$log.info(" -- Created Path: #{remotePath}") unless !ssh.createPath(remotePath)
ssh.chmod(remotePath, "777")
end
# Checksum the local path
localChecksum = Checksum::checksum(localPath)
$log.info(" -- Local Checksum: #{localChecksum}") unless !localChecksum
# Checksum the remote path
rcs = Checksum::SshChecksum.new(ssh)
remoteChecksum = rcs.checksum(File.join(remotePath, filename))
$log.info(" -- Remote Checksum: #{remoteChecksum}") unless !remoteChecksum
# If they match, we're done, just log it!
if localChecksum.eql?(remoteChecksum)
$log.warn(" -- Tried to copy existing file with matching checksum: #{localPath}")
return true
end
copyResult = ssh.copy(localPath, remotePath)
ssh.close
copyResult
end
end