file_pool 0.3.8 → 0.3.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/file_pool/version.rb +1 -1
  2. data/lib/file_pool.rb +97 -16
  3. metadata +4 -4
@@ -1,3 +1,3 @@
1
1
  module FilePool
2
- VERSION = "0.3.8"
2
+ VERSION = "0.3.9"
3
3
  end
data/lib/file_pool.rb CHANGED
@@ -167,7 +167,7 @@ module FilePool
167
167
  if @@crypted_mode
168
168
  if options[:decrypt]
169
169
  # return path of decrypted file (tmp path)
170
- decrypt id2dir_secured(fid) + "/#{fid}"
170
+ decrypt_to_file id2dir_secured(fid) + "/#{fid}"
171
171
  else
172
172
  id2dir_secured(fid) + "/#{fid}"
173
173
  end
@@ -187,6 +187,33 @@ module FilePool
187
187
  end
188
188
  end
189
189
 
190
+ # Returns an IO object providing an (unencrypted) stream of data of the given file ID
191
+ # To get the stream of the encrypted data pass :decrypt => false, as an option.
192
+ #
193
+ # === Parameters:
194
+ #
195
+ # fid (String)::
196
+ # File ID which was generated by a previous #add operation.
197
+ #
198
+ # options (Hash)::
199
+ # :decrypt (true,false) In encryption mode don't decrypt, but prioved the encrypted data. Defaults to +true+.
200
+ #
201
+ # === Return Value:
202
+ #
203
+ # :: *IO*, IO stream open for reading
204
+ #
205
+ def self.stream fid, options={}
206
+ options[:decrypt] = true unless options[:decrypt] == false
207
+
208
+ if path = path(fid, :decrypt => false)
209
+ if @@crypted_mode and options[:decrypt]
210
+ decrypt_to_stream path
211
+ else
212
+ open(path)
213
+ end
214
+ end
215
+ end
216
+
190
217
  #
191
218
  # Remove a previously added file by its ID. Same as FilePool.remove,
192
219
  # but throws exceptions on failure.
@@ -298,7 +325,7 @@ module FilePool
298
325
  #
299
326
  # === Return Value:
300
327
  #
301
- # :: *String*Path and name of the crypted file.
328
+ # :: *String* Path and name of the crypted file.
302
329
  def self.crypt path
303
330
  # Crypt the file in the temp folder and copy after
304
331
  cipher = create_cipher
@@ -320,7 +347,7 @@ module FilePool
320
347
  end
321
348
 
322
349
  #
323
- # Decrypt a file and give a path to it.
350
+ # Decrypt file to a file in /tmp
324
351
  #
325
352
  # Returns the path to the decrypted file.
326
353
  #
@@ -330,26 +357,80 @@ module FilePool
330
357
  # path of the file to decrypt.
331
358
  #
332
359
  # === Return Value:
360
+ #
361
+ # :: *String* Path and name of the decrypted file.
362
+ def self.decrypt_to_file path
363
+ tmpfile = Tempfile.new 'FilePool-decrypt'
364
+
365
+ File.open(path) do |crpt|
366
+ decrypt_io crpt, tmpfile
367
+ end
368
+
369
+ tmpfile.open # re-open for reading, prevents early deletion of tempfile
370
+ tmpfile.path
371
+ end
372
+
333
373
  #
334
- # :: *String*Path and name of the crypted file.
335
- def self.decrypt path
336
- decipher = create_decipher
337
- # Now decrypt the data:
338
- output = Tempfile.new 'FilePool-decrypt'
374
+ # Decrypt file to a stream (IO) non-blocking by forking a child process.
375
+ #
376
+ # === Parameters:
377
+ #
378
+ # path (String)::
379
+ # path of the file to decrypt.
380
+ #
381
+ # === Return Value:
382
+ #
383
+ # :: *IO* decrypted data as stream
384
+ def self.decrypt_to_stream path
339
385
 
386
+ pipe_read, pipe_write = IO.pipe
387
+
388
+ child = fork do
389
+ # in child process: decrypting
390
+ pipe_read.close
391
+
392
+ # open encrypted file
393
+ File.open(path) do |crpt|
394
+ decrypt_io crpt, pipe_write
395
+ end
396
+
397
+ pipe_write.close
398
+ end
399
+
400
+ # in parent
401
+ Process.detach(child) # not waiting for it
402
+ pipe_write.close
403
+ pipe_read
404
+ end
405
+
406
+ #
407
+ # Decrypts given IO blockwise and returns IO of decrypted data
408
+ #
409
+ # === Parameters:
410
+ #
411
+ # in_io (IO)::
412
+ # provide encrypted data, must be open for reading
413
+ # out_io (IO)::
414
+ # provides decrypted data, must be open for writing
415
+ #
416
+ # === Retrun value:
417
+ #
418
+ # nil
419
+ def self.decrypt_io in_io, out_io
420
+ decipher = create_decipher
340
421
  buf = ''
341
422
 
342
- File.open(path) do |inf|
343
- while inf.read(@@block_size, buf)
344
- output << decipher.update(buf)
345
- output.flush
346
- output.fsync
423
+ while in_io.read(@@block_size, buf)
424
+ out_io << decipher.update(buf)
425
+ if out_io.is_a?(File)
426
+ # avoid memory buffering
427
+ out_io.flush
428
+ out_io.fsync
347
429
  end
348
- output << decipher.final
349
430
  end
350
431
 
351
- output.open # re-open for reading, prevents early deletion of tempfile
352
- output.path
432
+ out_io << decipher.final
433
+ nil
353
434
  end
354
435
 
355
436
  #
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: file_pool
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 1
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 3
9
- - 8
10
- version: 0.3.8
9
+ - 9
10
+ version: 0.3.9
11
11
  platform: ruby
12
12
  authors:
13
13
  - "robokopp (Robert Anni\xC3\xA9s)"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2018-10-22 00:00:00 +02:00
18
+ date: 2018-10-23 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency