Class: KindleFS::Filesystem

Inherits:
FuseFS::FuseDir
  • Object
show all
Defined in:
lib/kindlefs/filesystem.rb

Constant Summary collapse

ROOT_PATH =
/^\/$/
COLLECTIONS_PATH =
/^\/collections$/.freeze
DOCUMENTS_PATH =
/^\/documents$/.freeze
PICTURES_PATH =
/^\/pictures$/.freeze
COLLECTION_NAME =
/([A-Za-z0-9_\-\s'"\.]+)/i.freeze
COLLECTION_PATH =
/^#{COLLECTIONS_PATH.strip}\/#{COLLECTION_NAME.strip}$/.freeze
DOCUMENT_NAME =
/([A-Za-z0-9_\s,\.\[\]\(\)'&!\-]+\.(mobi|epub|rtf|pdf|azw|azw1)+)/i.freeze
COLLECTION_DOCUMENT_PATH =
/^#{COLLECTIONS_PATH.strip}\/#{COLLECTION_NAME.strip}\/#{DOCUMENT_NAME.strip}$/.freeze
UNASSOCIATED_DOCUMENT_PATH =
/^#{COLLECTIONS_PATH.strip}\/#{DOCUMENT_NAME.strip}$/.freeze
DOCUMENT_PATH =
/^#{DOCUMENTS_PATH.strip}\/#{DOCUMENT_NAME.strip}$/.freeze

Instance Method Summary collapse

Instance Method Details

#can_delete?(path) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/kindlefs/filesystem.rb', line 83

def can_delete?(path)
  puts "can_delete? #{path}"
  case path
  when DOCUMENTS_PATH, PICTURES_PATH, COLLECTIONS_PATH
    false
  else
    true
  end
rescue Exception => e
  puts e
  false
end

#can_mkdir?(path) ⇒ Boolean

Returns:

  • (Boolean)


105
106
107
108
109
110
111
112
113
# File 'lib/kindlefs/filesystem.rb', line 105

def can_mkdir?(path)
  puts "can_mkdir? #{path}"
  case path
  when COLLECTION_PATH
    true
  else
    false
  end
end

#can_rmdir?(path) ⇒ Boolean

Returns:

  • (Boolean)


115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/kindlefs/filesystem.rb', line 115

def can_rmdir?(path)
  puts "can_rmdir #{path}"
  case path
  when COLLECTION_PATH
    true
  else
    false
  end
rescue Exception => e
  puts e
  false
end

#can_write?(path) ⇒ Boolean

Returns:

  • (Boolean)


96
97
98
99
100
101
102
103
# File 'lib/kindlefs/filesystem.rb', line 96

def can_write?(path)
  puts "can_write? #{path}"
  if path !~ ROOT_PATH
    true
  else
    false
  end
end

#contents(path) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/kindlefs/filesystem.rb', line 17

def contents path
  case path
  when ROOT_PATH
    [ 'collections', 'documents', 'pictures' ]
  when COLLECTIONS_PATH
    Rindle::Collection.all.map(&:name) + Rindle::Document.unassociated.map(&:filename)
  when DOCUMENTS_PATH
    Rindle::Document.all.map(&:filename)
  when COLLECTION_PATH
    collection = Rindle::Collection.first :named => $1
    collection.documents.map(&:filename)
  else
    []
  end
rescue Exception => e
  puts e
  false
end

#delete(path) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/kindlefs/filesystem.rb', line 177

def delete path
  puts "delete #{path}"
  case path
  when COLLECTION_DOCUMENT_PATH
    col = Rindle::Collection.find_by_name $1
    doc = Rindle::Document.find_by_name $2
    col.remove doc
  when DOCUMENT_PATH, UNASSOCIATED_DOCUMENT_PATH
    doc = Rindle::Document.find_by_name $1
    doc.delete!
  end
  Rindle.save
  true
rescue Exception => e
  puts e
  false
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/kindlefs/filesystem.rb', line 55

def directory?(path)
  case path
  when COLLECTIONS_PATH, DOCUMENTS_PATH, PICTURES_PATH
    true
  when COLLECTION_PATH
    col = Rindle::Collection.find_by_name $1
    !col.nil?
  else
    false
  end
rescue Exception => e
  puts e
  false
end

#executable?(path) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/kindlefs/filesystem.rb', line 70

def executable? path
  false
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/kindlefs/filesystem.rb', line 36

def file?(path)
  case path
  when DOCUMENT_PATH
    doc = Rindle::Document.find_by_name $1
    return !doc.nil?
  when COLLECTION_DOCUMENT_PATH
    col = Rindle::Collection.find_by_name $1
    doc = Rindle::Document.find_by_name $2
    return doc && col.include?(doc)
  when UNASSOCIATED_DOCUMENT_PATH
    doc = Rindle::Document.find_by_name $1
    return doc.collections.empty? if doc
  end
  false
rescue Exception => e
  puts e
  false
end

#mkdir(path) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kindlefs/filesystem.rb', line 146

def mkdir(path)
  puts "mkdir #{path}"
  if path =~ COLLECTION_PATH
    col = Rindle::Collection.find_by_name $1
    Rindle::Collection.create($1) if col.nil?
    Rindle.save
    true
  else
    false
  end
rescue Exception => e
  puts e
  false
end

#read_file(path) ⇒ Object



261
262
263
264
265
266
267
# File 'lib/kindlefs/filesystem.rb', line 261

def read_file path
  doc = Rindle::Document.find_by_name File.basename(path)
  doc.filename
rescue Exception => e
  puts "read file error: #{e}"
  ''
end

#rename(old, new) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
# File 'lib/kindlefs/filesystem.rb', line 195

def rename old, new
  case old
  when UNASSOCIATED_DOCUMENT_PATH
    doc = Rindle::Document.find_by_name $1
    if new =~ COLLECTION_DOCUMENT_PATH
      col = Rindle::Collection.find_by_name $1
      col.add doc
    end
    if doc and !doc.amazon?
      doc.rename! File.basename(new)
    else
      return false
    end
  when COLLECTION_DOCUMENT_PATH
    old_col = Rindle::Collection.find_by_name $1
    doc = Rindle::Document.find_by_name $2
    if new =~ COLLECTION_DOCUMENT_PATH
      new_col = Rindle::Collection.find_by_name $1
      unless old_col == new_col
        old_col.remove doc
        new_col.add doc
      end
      doc.rename! File.basename(new)
    else
      return false
    end
  when COLLECTION_PATH
    collection = Rindle::Collection.find_by_name $1
    if new =~ COLLECTION_PATH
      collection.rename! File.basename(new)
    else
      return false
    end
  else
    return false
  end
  Rindle.save
  true
rescue Exception => e
  puts e
  false
end

#rmdir(path) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/kindlefs/filesystem.rb', line 161

def rmdir(path)
  puts "rmdir #{path}"
  if path =~ COLLECTION_PATH
    collection = Rindle::Collection.first :named => $1
    return false unless collection
    collection.destroy!
  else
    return false
  end
  Rindle.save
  true
rescue Exception => e
  puts e
  false
end

#size(path) ⇒ Object



74
75
76
77
78
79
80
81
# File 'lib/kindlefs/filesystem.rb', line 74

def size path
  puts "size #{path}"
  doc = Rindle::Document.find_by_name File.basename(path)
  File.size File.join(Rindle.root_path, doc.path)
rescue Exception => e
  puts e
  false
end

#touch(path, val = 0) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/kindlefs/filesystem.rb', line 128

def touch(path, val = 0)
  puts "touch #{path}"
  filename = File.basename(path)
  case path
  when UNASSOCIATED_DOCUMENT_PATH, DOCUMENT_PATH
    doc = Rindle::Document.find_by_name $1
    doc = Rindle::Document.create $1 unless doc
    FileUtils.touch(File.join(Rindle.root_path, doc.path))
    Rindle.save
    true
  else
    false
  end
rescue Exception => e
  puts e
  false
end

#write_to(path, body) ⇒ Object



238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/kindlefs/filesystem.rb', line 238

def write_to path, body
  doc = Rindle::Document.find_by_name File.basename(path)
  if doc
    if path =~ COLLECTION_DOCUMENT_PATH
      col = Rindle::Collection.find_by_name $1
      col.add doc
    end
  else
    doc = Rindle::Document.create File.basename(path)
    File.open(File.join(Rindle.root_path, doc.path), 'w+') do |f|
      f.write body
    end
    if path =~ COLLECTION_DOCUMENT_PATH
      Rindle::Collection.find_by_name($1).add doc
    end
  end
  Rindle.save
  true
rescue Exception => e
  puts e
  false
end