aboutsummaryrefslogtreecommitdiffstats
path: root/app/services/file_service.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/services/file_service.rb')
-rw-r--r--app/services/file_service.rb23
1 files changed, 23 insertions, 0 deletions
diff --git a/app/services/file_service.rb b/app/services/file_service.rb
new file mode 100644
index 000000000..efccbe24f
--- /dev/null
+++ b/app/services/file_service.rb
@@ -0,0 +1,23 @@
+module FileService extend self
+
+ def unique_filename( path, enum_with: with_ints )
+ file_names = enum_with.map( &file_name_maker(path) )
+ file_names
+ .drop_while( &File.method(:exists?) )
+ .next
+ end
+
+ def with_ints(format='%d')
+ (0..Float::INFINITY)
+ .lazy
+ .map{ |n| format % n }
+ end
+
+
+ private
+
+ def file_name_maker path
+ ->(n){ [path, n].join('_') }
+ end
+
+end