Skip to content

Commit

Permalink
Merge branch 'release/1.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Siri committed Feb 17, 2018
2 parents 9f43fd4 + 394a30c commit 5139416
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 19 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ by adding `upstream` to your list of dependencies in `mix.exs`:

```elixir
def deps do
[{:upstream, "~> 1.2.0"}]
[{:upstream, "~> 1.3.0"}]
end
```

Expand Down
20 changes: 20 additions & 0 deletions lib/upstream/b2/large_file/list_parts.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
defmodule Upstream.B2.LargeFile.ListParts do
@moduledoc """
This module will get the parts of an unfinished large file
"""

defstruct [
:parts,
:next_part_number
]

use Upstream.B2.Base

def url(_), do: Url.generate(Account.api_url(), :list_parts)

def body(file_id) when is_binary(file_id), do: %{fileId: file_id}

def extract_shas(parts) do
Enum.map(parts, fn part -> part["contentSha1"] end)
end
end
9 changes: 8 additions & 1 deletion lib/upstream/b2/large_file/unfinished.ex
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
defmodule Upstream.B2.LargeFile.Unfinished do
@moduledoc """
This module retrieves Unfinished LargeFiles
## Examples
iex> Upstream.B2.LargeFile.Unfinished.call
{:ok, %Upstream.B2.LargeFile.Unfinished{}}
"""
defstruct [:files, :next_file_id]

@type t() :: %__MODULE__{
Expand All @@ -8,7 +15,7 @@ defmodule Upstream.B2.LargeFile.Unfinished do

use Upstream.B2.Base

def url(_), do: Account.api_url() |> Url.generate(:list_unfinished_large_files)
def url(_), do: Url.generate(Account.api_url(), :list_unfinished_large_files)

def body(_) do
%{bucketId: Upstream.config(:bucket_id)}
Expand Down
3 changes: 3 additions & 0 deletions lib/upstream/request.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ defmodule Upstream.Request do

{:ok, %{status_code: _, body: body}} ->
{:error, struct(Error, process_response(body))}

{:error, %HTTPoison.Error{id: _id, reason: reason}} ->
{:error, %{error: reason}}
end
end

Expand Down
29 changes: 27 additions & 2 deletions lib/upstream/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@ defmodule Upstream.Router do

plug(:dispatch)

get "/:prefix/*path" do
get "/chunks/unfinished" do
case B2.LargeFile.Unfinished.call do
{:ok, unfinished} ->
render_json(conn, 200, unfinished)

{:error, reason} ->
render_json(conn, 422, reason)
end
end

get "/chunks/resume/:file_id" do
case B2.LargeFile.ListParts.call(body: file_id) do
{:ok, %B2.LargeFile.ListParts{parts: parts}} ->
shas = B2.LargeFile.ListParts.extract_shas(parts)
render_json(conn, 200, %{shas: shas})

{:error, reason} ->
render_json(conn, 422, reason)
end
end

get "/source/:prefix/*path" do
render_json(conn, 200, %{
sequences: [
%{
Expand Down Expand Up @@ -82,11 +103,15 @@ defmodule Upstream.Router do
end
end

require IEx
patch "/chunks/add" do
IEx.pry

%{"file_id" => file_id, "part_number" => part_number, "chunk_size" => chunk_size} =
conn.body_params

%{path: path, filename: _filename} = conn.body_params[Upstream.file_param()]
%{path: path,
filename: _filename} = conn.body_params[Upstream.file_param()]

upload_params = %{
file_id: file_id,
Expand Down
31 changes: 17 additions & 14 deletions lib/upstream/worker/chunk.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,27 @@ defmodule Upstream.Worker.Chunk do
use Upstream.Worker.Base

def task(state) do
{:ok, checksum} = Checksum.start_link()
{:ok, part_url} = Upload.part_url(state.uid.file_id)
with {:ok, checksum} <- Checksum.start_link(),
{:ok, part_url} <- Upload.part_url(state.uid.file_id) do

index = state.uid.index
index = state.uid.index

header = %{
authorization: part_url.authorization_token,
x_bz_part_number: index + 1,
content_length: state.job.content_length + 40,
x_bz_content_sha1: "hex_digits_at_end"
}
header = %{
authorization: part_url.authorization_token,
x_bz_part_number: index + 1,
content_length: state.job.content_length + 40,
x_bz_content_sha1: "hex_digits_at_end"
}

body = Flow.generate(state.job.stream, index, checksum)
body = Flow.generate(state.job.stream, index, checksum)

try do
Upload.part(part_url.upload_url, header, body)
after
Checksum.stop(checksum)
try do
Upload.part(part_url.upload_url, header, body)
after
Checksum.stop(checksum)
end
else
{:error, reason} -> {:error, reason}
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Upstream.Mixfile do
def project do
[
app: :upstream,
version: "1.2.3",
version: "1.3.0",
elixir: "~> 1.6.0",
build_embedded: Mix.env() == :prod,
start_permanent: Mix.env() == :prod,
Expand Down

0 comments on commit 5139416

Please sign in to comment.