Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a uri method to FileDescriptor #756

Merged
merged 1 commit into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions io/src/main/scala/com/kevel/apso/io/FileDescriptor.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kevel.apso.io

import java.io.InputStream
import java.net.URI

import scala.io.Source

Expand All @@ -15,6 +16,13 @@ trait FileDescriptor {
*/
def path: String

/** Returns a Uniform Resource Identifier (URI) that references this file's location.
*
* @return
* the URI that references this file's location.
*/
def uri: URI

/** The name of the file associated with the file descriptor.
* @return
* the file name.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kevel.apso.io

import java.io.{FileInputStream, FileWriter, InputStream}
import java.net.URI
import java.nio.file.{Files, Path, Paths, StandardCopyOption}

import scala.io.Source
Expand All @@ -21,6 +22,9 @@ case class LocalFileDescriptor(initialPath: String) extends FileDescriptor with

lazy val path: String = file.getAbsolutePath

def uri: URI =
new URI(s"file://$path")

lazy val name: String = file.getName

val isLocal: Boolean = true
Expand Down Expand Up @@ -208,7 +212,8 @@ case class LocalFileDescriptor(initialPath: String) extends FileDescriptor with
*/
def readString: String = Source.fromFile(file, "UTF-8").mkString

override def toString: String = s"file://$path"
override def toString: String =
uri.toString

override def equals(other: Any): Boolean = other match {
case that: LocalFileDescriptor => path == that.path
Expand Down
7 changes: 6 additions & 1 deletion io/src/main/scala/com/kevel/apso/io/S3FileDescriptor.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kevel.apso.io

import java.io.InputStream
import java.net.URI

import scala.collection.concurrent.TrieMap

Expand Down Expand Up @@ -29,6 +30,9 @@ case class S3FileDescriptor(
protected def duplicate(elements: List[String]) =
this.copy(elements = elements)

def uri: URI =
new URI(s"s3://$path")

def size = summary match {
case Some(info) => info.getSize
case None => bucket.size(builtPath)
Expand Down Expand Up @@ -134,7 +138,8 @@ case class S3FileDescriptor(
result
}

override def toString: String = s"s3://$path"
override def toString: String =
uri.toString
}

object S3FileDescriptor {
Expand Down
8 changes: 6 additions & 2 deletions io/src/main/scala/com/kevel/apso/io/SftpFileDescriptor.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kevel.apso.io

import java.io.{FileDescriptor => _, _}
import java.net.URI
import java.util.concurrent.{ConcurrentHashMap, TimeoutException}

import scala.concurrent.duration._
Expand Down Expand Up @@ -100,6 +101,10 @@ case class SftpFileDescriptor(
withFileAttributes(f)
}

def uri: URI =
if (port != 22) new URI(s"sftp://$username@$host:$port$path")
else new URI(s"sftp://$username@$host$path")

def size = withFileAttributes(_.getSize)

def lastModifiedTimestamp = withFileAttributes(_.getMtime())
Expand Down Expand Up @@ -200,8 +205,7 @@ case class SftpFileDescriptor(
}

override def toString: String =
if (port != 22) s"sftp://$username@$host:$port$path"
else s"sftp://$username@$host$path"
uri.toString

override def equals(other: Any): Boolean = other match {
case that: SftpFileDescriptor =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.kevel.apso.io

import java.io.{File, InputStream}
import java.net.URI
import java.nio.file.Files
import java.util.UUID

Expand All @@ -23,6 +24,13 @@ class LocalFileDescriptorSpec extends Specification {
file.getAbsolutePath == fd.path
}

"have a correct URI that exposes the full path" in {
val file = new File("/tmp/one/two/three")
val fd = LocalFileDescriptor("/tmp/one/two/three")
fd.uri ==== new URI("file:///tmp/one/two/three")
file.toURI() ==== fd.uri
}

"retrieve the size of a file" in {
val fd1 = LocalFileDescriptor("/tmp") / randomFolder / randomString
fd1.write("hello world")
Expand Down
14 changes: 14 additions & 0 deletions io/src/test/scala/com/kevel/apso/io/S3FileDescriptorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.kevel.apso.io

import java.net.URI

import org.specs2.mutable.Specification

class S3FileDescriptorSpec extends Specification {
"A S3FileDescriptor" should {
"have a correct URI that exposes the full path" in {
val file = S3FileDescriptor("bucket/key")
file.uri ==== new URI("s3://bucket/key")
}
}
}
19 changes: 19 additions & 0 deletions io/src/test/scala/com/kevel/apso/io/SftpFileDescriptorSpec.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.kevel.apso.io

import java.net.URI

import org.specs2.mutable.Specification

import com.kevel.apso.io.config.Credentials

class SftpFileDescriptorSpec extends Specification {
"A SftpFileDescriptor" should {
"have a correct URI that exposes the full path" in {
val file = SftpFileDescriptor(
"localhost/tmp/file",
Credentials.Sftp(default = Some(Credentials.Sftp.Entry.Basic(username = "user123", password = "pass456")))
)
file.uri ==== new URI("sftp://user123@localhost/tmp/file")
}
}
}