├── .tool-versions
├── .DS_Store
├── lib
├── ephemeral_share_web
│ ├── templates
│ │ ├── page
│ │ │ └── index.html.eex
│ │ └── layout
│ │ │ └── app.html.eex
│ ├── views
│ │ ├── layout_view.ex
│ │ ├── page_view.ex
│ │ ├── error_view.ex
│ │ └── error_helpers.ex
│ ├── controllers
│ │ └── page_controller.ex
│ ├── channels
│ │ ├── broker_channel.ex
│ │ ├── peer_socket.ex
│ │ ├── broker_socket.ex
│ │ └── peer_channel.ex
│ ├── router.ex
│ ├── gettext.ex
│ └── endpoint.ex
├── ephemeral_share
│ ├── repo.ex
│ ├── peer_manager.ex
│ ├── peer_state.ex
│ └── application.ex
├── ephemeral_share.ex
└── ephemeral_share_web.ex
├── assets
├── .DS_Store
├── js
│ ├── .DS_Store
│ ├── constants
│ │ ├── PeerCommunicationConstants.js
│ │ ├── fileconstants.js
│ │ └── filetransferconstants.js
│ ├── actions
│ │ ├── filetransferaction.js
│ │ └── fileactions.js
│ ├── appdispatcher.js
│ ├── components
│ │ ├── errorbanner.jsx
│ │ ├── itemslist.jsx
│ │ ├── filehandler.jsx
│ │ ├── peerconnectionstatus.jsx
│ │ ├── listitem.jsx
│ │ └── filelistitem.jsx
│ ├── app.jsx
│ ├── lib
│ │ ├── filetransfersender.js
│ │ ├── filetransferreceiver.js
│ │ ├── filetransfermanager.js
│ │ └── peercommunication.js
│ └── stores
│ │ └── fileinfostore.js
├── static
│ ├── favicon.ico
│ ├── images
│ │ └── phoenix.png
│ └── robots.txt
├── css
│ ├── app.css
│ └── phoenix.css
├── .babelrc
├── package.json
└── webpack.config.js
├── priv
├── repo
│ ├── migrations
│ │ └── .formatter.exs
│ └── seeds.exs
└── gettext
│ ├── en
│ └── LC_MESSAGES
│ │ └── errors.po
│ └── errors.pot
├── test
├── test_helper.exs
├── views
│ ├── page_view_test.exs
│ └── error_view_test.exs
├── ephemeral_share_web
│ ├── views
│ │ ├── page_view_test.exs
│ │ ├── layout_view_test.exs
│ │ └── error_view_test.exs
│ └── controllers
│ │ └── page_controller_test.exs
├── controllers
│ └── page_controller_test.exs
└── support
│ ├── channel_case.ex
│ ├── conn_case.ex
│ ├── data_case.ex
│ └── model_case.ex
├── .formatter.exs
├── package.json
├── config
├── test.exs
├── config.exs
├── dev.exs
└── prod.exs
├── Dockerfile
├── README.md
├── .gitignore
├── mix.exs
└── mix.lock
/.tool-versions:
--------------------------------------------------------------------------------
1 | erlang 22.0
2 | elixir 1.9.1
3 | nodejs 10.15.3
4 |
--------------------------------------------------------------------------------
/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zabirauf/ephemeral_share/HEAD/.DS_Store
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/templates/page/index.html.eex:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/assets/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zabirauf/ephemeral_share/HEAD/assets/.DS_Store
--------------------------------------------------------------------------------
/assets/js/.DS_Store:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zabirauf/ephemeral_share/HEAD/assets/js/.DS_Store
--------------------------------------------------------------------------------
/priv/repo/migrations/.formatter.exs:
--------------------------------------------------------------------------------
1 | [
2 | import_deps: [:ecto_sql],
3 | inputs: ["*.exs"]
4 | ]
5 |
--------------------------------------------------------------------------------
/test/test_helper.exs:
--------------------------------------------------------------------------------
1 | ExUnit.start()
2 | Ecto.Adapters.SQL.Sandbox.mode(EphemeralShare.Repo, :manual)
3 |
--------------------------------------------------------------------------------
/assets/static/favicon.ico:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zabirauf/ephemeral_share/HEAD/assets/static/favicon.ico
--------------------------------------------------------------------------------
/assets/static/images/phoenix.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zabirauf/ephemeral_share/HEAD/assets/static/images/phoenix.png
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/views/layout_view.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.LayoutView do
2 | use EphemeralShareWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/views/page_view.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.PageView do
2 | use EphemeralShareWeb, :view
3 | end
4 |
--------------------------------------------------------------------------------
/test/views/page_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PageViewTest do
2 | use EphemeralShare.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/assets/css/app.css:
--------------------------------------------------------------------------------
1 | /* This file is for your main application css. */
2 |
3 | @import "./phoenix.css";
4 | @import "./materialize.scss";
5 |
--------------------------------------------------------------------------------
/test/ephemeral_share_web/views/page_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.PageViewTest do
2 | use EphemeralShareWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/test/ephemeral_share_web/views/layout_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.LayoutViewTest do
2 | use EphemeralShareWeb.ConnCase, async: true
3 | end
4 |
--------------------------------------------------------------------------------
/lib/ephemeral_share/repo.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.Repo do
2 | use Ecto.Repo,
3 | otp_app: :ephemeral_share,
4 | adapter: Ecto.Adapters.Postgres
5 | end
6 |
--------------------------------------------------------------------------------
/.formatter.exs:
--------------------------------------------------------------------------------
1 | [
2 | import_deps: [:ecto, :phoenix],
3 | inputs: ["*.{ex,exs}", "priv/*/seeds.exs", "{config,lib,test}/**/*.{ex,exs}"],
4 | subdirectories: ["priv/*/migrations"]
5 | ]
6 |
--------------------------------------------------------------------------------
/assets/.babelrc:
--------------------------------------------------------------------------------
1 | {
2 | "presets": ["@babel/preset-env", "@babel/preset-react"],
3 | "plugins": [
4 | "@babel/plugin-proposal-class-properties",
5 | "@babel/plugin-proposal-object-rest-spread"
6 | ]
7 | }
8 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/controllers/page_controller.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.PageController do
2 | use EphemeralShareWeb, :controller
3 |
4 | def index(conn, _params) do
5 | render(conn, "index.html")
6 | end
7 | end
8 |
--------------------------------------------------------------------------------
/assets/static/robots.txt:
--------------------------------------------------------------------------------
1 | # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2 | #
3 | # To ban all spiders from the entire site uncomment the next two lines:
4 | # User-agent: *
5 | # Disallow: /
6 |
--------------------------------------------------------------------------------
/assets/js/constants/PeerCommunicationConstants.js:
--------------------------------------------------------------------------------
1 |
2 | module.exports = {
3 | CONNECTED: "connected-broker",
4 | PEER_DATA: "peer-data-received",
5 | PEER_CONNECTED: "peer-connected",
6 | PEER_DOES_NOT_EXIST: "peer-does-not-exist"
7 | };
8 |
--------------------------------------------------------------------------------
/test/controllers/page_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PageControllerTest do
2 | use EphemeralShare.ConnCase
3 |
4 | test "GET /" do
5 | conn = get conn(), "/"
6 | assert html_response(conn, 200) =~ "Welcome to Phoenix!"
7 | end
8 | end
9 |
--------------------------------------------------------------------------------
/assets/js/constants/fileconstants.js:
--------------------------------------------------------------------------------
1 |
2 | module.exports = {
3 | FILE_CREATE: "file_create",
4 | FILE_DESTROY: "file_destroy",
5 | FILE_DOWNLOAD: "file_download",
6 | FILE_DOWNLOAD_COMPLETE: "file_download_complete",
7 | FILE_DOWNLOAD_PROGRESS: "file_download_progress"
8 | };
9 |
--------------------------------------------------------------------------------
/test/ephemeral_share_web/controllers/page_controller_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.PageControllerTest do
2 | use EphemeralShareWeb.ConnCase
3 |
4 | test "GET /", %{conn: conn} do
5 | conn = get(conn, "/")
6 | assert html_response(conn, 200) =~ "Welcome to Phoenix!"
7 | end
8 | end
9 |
--------------------------------------------------------------------------------
/lib/ephemeral_share.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare do
2 | @moduledoc """
3 | EphemeralShare keeps the contexts that define your domain
4 | and business logic.
5 |
6 | Contexts are also responsible for managing your data, regardless
7 | if it comes from the database, an external API or others.
8 | """
9 | end
10 |
--------------------------------------------------------------------------------
/priv/repo/seeds.exs:
--------------------------------------------------------------------------------
1 | # Script for populating the database. You can run it as:
2 | #
3 | # mix run priv/repo/seeds.exs
4 | #
5 | # Inside the script, you can read and write to any of your
6 | # repositories directly:
7 | #
8 | # EphemeralShare.Repo.insert!(%EphemeralShare.SomeSchema{})
9 | #
10 | # We recommend using the bang functions (`insert!`, `update!`
11 | # and so on) as they will fail if something goes wrong.
12 |
--------------------------------------------------------------------------------
/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "repository": {},
3 | "dependencies": {
4 | "alt": "^0.17.1",
5 | "babel-brunch": "^5.1.1",
6 | "brunch": "^1.8.1",
7 | "clean-css-brunch": ">= 1.0 < 1.8",
8 | "css-brunch": ">= 1.0 < 1.8",
9 | "flux": "^2.0.3",
10 | "javascript-brunch": ">= 1.0 < 1.8",
11 | "react": "^0.13.3",
12 | "react-brunch": "^1.0.9",
13 | "sass-brunch": "^1.8.10",
14 | "simple-peer": "^5.11.4",
15 | "uglify-js-brunch": ">= 1.0 < 1.8"
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/lib/ephemeral_share/peer_manager.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PeerManager do
2 |
3 | def add_peer(peer_id) do
4 | EphemeralShare.PeerState.start_link(peer_id)
5 | end
6 |
7 | def exists?(peer_id) do
8 | exists = EphemeralShare.PeerState.peer_state_proc_id(peer_id)
9 | |> :global.whereis_name
10 |
11 | case exists do
12 | :undefined -> false
13 | _ -> true
14 | end
15 | end
16 |
17 | def remove_peer(peer_id) do
18 | EphemeralShare.PeerState.stop(peer_id)
19 | end
20 | end
21 |
--------------------------------------------------------------------------------
/test/ephemeral_share_web/views/error_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.ErrorViewTest do
2 | use EphemeralShareWeb.ConnCase, async: true
3 |
4 | # Bring render/3 and render_to_string/3 for testing custom views
5 | import Phoenix.View
6 |
7 | test "renders 404.html" do
8 | assert render_to_string(EphemeralShareWeb.ErrorView, "404.html", []) == "Not Found"
9 | end
10 |
11 | test "renders 500.html" do
12 | assert render_to_string(EphemeralShareWeb.ErrorView, "500.html", []) == "Internal Server Error"
13 | end
14 | end
15 |
--------------------------------------------------------------------------------
/config/test.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # We don't run a server during test. If one is required,
4 | # you can enable the server option below.
5 | config :ephemeral_share, EphemeralShareWeb.Endpoint,
6 | http: [port: 4002],
7 | server: false
8 |
9 | # Print only warnings and errors during test
10 | config :logger, level: :warn
11 |
12 | # Configure your database
13 | config :ephemeral_share, EphemeralShare.Repo,
14 | username: "postgres",
15 | password: "postgres",
16 | database: "ephemeral_share_test",
17 | hostname: "localhost",
18 | pool: Ecto.Adapters.SQL.Sandbox
19 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/views/error_view.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.ErrorView do
2 | use EphemeralShareWeb, :view
3 |
4 | # If you want to customize a particular status code
5 | # for a certain format, you may uncomment below.
6 | # def render("500.html", _assigns) do
7 | # "Internal Server Error"
8 | # end
9 |
10 | # By default, Phoenix returns the status message from
11 | # the template name. For example, "404.html" becomes
12 | # "Not Found".
13 | def template_not_found(template, _assigns) do
14 | Phoenix.Controller.status_message_from_template(template)
15 | end
16 | end
17 |
--------------------------------------------------------------------------------
/Dockerfile:
--------------------------------------------------------------------------------
1 | FROM elixir:1.9.1
2 |
3 | RUN mkdir /app
4 | COPY . /app
5 | WORKDIR /app
6 |
7 | RUN mix local.hex --force \
8 | && mix archive.install --force hex phx_new 1.4.9 \
9 | && apt-get update \
10 | && curl -sL https://deb.nodesource.com/setup_10.x | bash \
11 | && apt-get install -y apt-utils \
12 | && apt-get install -y nodejs \
13 | && apt-get install -y build-essential \
14 | && mix local.rebar --force
15 |
16 | EXPOSE 4000
17 |
18 | RUN MIX_ENV=prod mix compile \
19 | && npm run deploy --prefix ./assets \
20 | && mix phx.digest
21 |
22 | CMD PORT=4001 MIX_ENV=prod mix phx.server
--------------------------------------------------------------------------------
/assets/js/actions/filetransferaction.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import {AppDispatcher} from "../appdispatcher";
4 | import FileTransferConstants from "../constants/filetransferconstants";
5 |
6 | /**
7 | * Actions for file transfer
8 | */
9 | export class FileTransferActions {
10 |
11 | /**
12 | * Download the file
13 | */
14 | static download(file, correlationId) {
15 | AppDispatcher.handleStoreAction({
16 | actionType: FileTransferConstants.TRANSFER_FILE_DOWNLOAD,
17 | file: file,
18 | correlationId: correlationId
19 | });
20 | }
21 | }
22 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/channels/broker_channel.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.BrokerChannel do
2 | use Phoenix.Channel
3 |
4 | require Logger
5 |
6 | def join("broker:match", _auth_msg, socket) do
7 | {:ok, socket}
8 | end
9 |
10 | def join("broker:" <> _sub_topic, _auth_msg, _socket) do
11 | {:error, %{reason: "Invalid subchannel"}}
12 | end
13 |
14 | def terminate(_msg, _socket) do
15 | {:shutdown, :left}
16 | end
17 |
18 | def handle_in("register", %{}, socket) do
19 | id = socket.assigns.peer_id
20 | push(socket, "registered", %{id: id})
21 | {:noreply, socket}
22 | end
23 | end
24 |
--------------------------------------------------------------------------------
/assets/js/constants/filetransferconstants.js:
--------------------------------------------------------------------------------
1 |
2 | module.exports = {
3 | // File transfer related events
4 | TRANSFER_FILE_DOWNLOAD: "transfer_file_download",
5 | TRANSFER_FILE_DOWNLOAD_PROGRESS: "transfer_file_download_progress",
6 | TRANSFER_FILE_DOWNLOAD_COMPLETE: "transfer_file_download_complete",
7 | TRANSFER_FILE_UPLOAD_PROGRESS: "transfer_file_upload_progress",
8 | TRANSFER_FILE_UPLOAD_COMPLETE: "transfer_file_upload_complete",
9 |
10 | // Other file transfer related constants
11 | // Currently set to 1 to have reliable transfer
12 | // as each chunk will be acked, but will be slow
13 | TRANSFER_RATE: 1
14 | };
15 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/router.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.Router do
2 | use EphemeralShareWeb, :router
3 |
4 | pipeline :browser do
5 | plug :accepts, ["html"]
6 | plug :fetch_session
7 | plug :fetch_flash
8 | plug :protect_from_forgery
9 | plug :put_secure_browser_headers
10 | end
11 |
12 | pipeline :api do
13 | plug :accepts, ["json"]
14 | end
15 |
16 | scope "/", EphemeralShareWeb do
17 | pipe_through :browser
18 |
19 | get "/", PageController, :index
20 | end
21 |
22 | # Other scopes may use custom stacks.
23 | # scope "/api", EphemeralShareWeb do
24 | # pipe_through :api
25 | # end
26 | end
27 |
--------------------------------------------------------------------------------
/assets/js/appdispatcher.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import { Dispatcher } from "flux";
4 |
5 | const dispatcher = new Dispatcher();
6 |
7 | export class AppDispatcher {
8 | static register(callback) {
9 | return dispatcher.register(callback);
10 | }
11 |
12 | static dispatch(action) {
13 | dispatcher.dispatch(action);
14 | }
15 |
16 | static handleViewAction(action) {
17 | dispatcher.dispatch({
18 | source: "VIEW_ACTION",
19 | action: action
20 | });
21 | }
22 |
23 | static handleStoreAction(action) {
24 | dispatcher.dispatch({
25 | source: "STORE_ACTION",
26 | action: action
27 | });
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/assets/js/components/errorbanner.jsx:
--------------------------------------------------------------------------------
1 | import * as React from "react";
2 |
3 | export class ErrorBanner extends React.Component {
4 | constructor(props) {
5 | super(props);
6 | }
7 |
8 | render() {
9 | return (
10 |
11 |
12 |
13 |
14 | error
15 |
18 |
19 |
20 |
21 |
22 | );
23 | }
24 | }
25 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # EphemeralShare
2 |
3 | [](https://semaphoreci.com/zabirauf/ephemeral_share)
4 | []
5 |
6 | To start your new Phoenix application:
7 |
8 | 1. Install dependencies with `mix deps.get`
9 | 2. Start Phoenix endpoint with `mix phx.server`
10 |
11 | Now you can visit `localhost:4000` from your browser.
12 |
13 |
14 | ## Docker
15 |
16 | You can run the application using the docker file.
17 |
18 | 1. Build image `docker build . -t ephemeral_share:0.1.0`
19 | 2. Run container `docker run -it -p 4000:4001 ephemeral_share:0.1.0`
--------------------------------------------------------------------------------
/test/views/error_view_test.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.ErrorViewTest do
2 | use EphemeralShare.ConnCase, async: true
3 |
4 | # Bring render/3 and render_to_string/3 for testing custom views
5 | import Phoenix.View
6 |
7 | test "renders 404.html" do
8 | assert render_to_string(EphemeralShare.ErrorView, "404.html", []) ==
9 | "Page not found"
10 | end
11 |
12 | test "render 500.html" do
13 | assert render_to_string(EphemeralShare.ErrorView, "500.html", []) ==
14 | "Server internal error"
15 | end
16 |
17 | test "render any other" do
18 | assert render_to_string(EphemeralShare.ErrorView, "505.html", []) ==
19 | "Server internal error"
20 | end
21 | end
22 |
--------------------------------------------------------------------------------
/lib/ephemeral_share/peer_state.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PeerState do
2 | require Logger
3 | use GenServer
4 |
5 | def start_link(peer_id) do
6 | GenServer.start_link(__MODULE__, [peer_id], name: {:global, peer_state_proc_id(peer_id)})
7 | end
8 |
9 | def stop(peer_id) do
10 | peer_id
11 | |> peer_state_proc_id
12 | |> :global.whereis_name()
13 | |> GenServer.cast(:shutdown)
14 | end
15 |
16 | def peer_state_proc_id(peer_id) do
17 | "peer_state:" <> peer_id
18 | end
19 |
20 | # Server (callbacks)
21 |
22 | def init(args) do
23 | {:ok, args}
24 | end
25 |
26 | def handle_cast(:shutdown, state) do
27 | Logger.debug("Shutdown peer state #{inspect(state)}")
28 | {:stop, :shutdown, state}
29 | end
30 | end
31 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/gettext.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.Gettext do
2 | @moduledoc """
3 | A module providing Internationalization with a gettext-based API.
4 |
5 | By using [Gettext](https://hexdocs.pm/gettext),
6 | your module gains a set of macros for translations, for example:
7 |
8 | import EphemeralShareWeb.Gettext
9 |
10 | # Simple translation
11 | gettext("Here is the string to translate")
12 |
13 | # Plural translation
14 | ngettext("Here is the string to translate",
15 | "Here are the strings to translate",
16 | 3)
17 |
18 | # Domain-based translation
19 | dgettext("errors", "Here is the error message to translate")
20 |
21 | See the [Gettext Docs](https://hexdocs.pm/gettext) for detailed usage.
22 | """
23 | use Gettext, otp_app: :ephemeral_share
24 | end
25 |
--------------------------------------------------------------------------------
/assets/js/actions/fileactions.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import {AppDispatcher} from "../appdispatcher";
4 | import FileConstants from "../constants/fileconstants";
5 |
6 | /**
7 | * Actions for file store
8 | */
9 | export class FileActions {
10 |
11 | /**
12 | * Create a file
13 | */
14 | static create(file) {
15 | AppDispatcher.handleViewAction({
16 | actionType: FileConstants.FILE_CREATE,
17 | file: file
18 | });
19 | }
20 |
21 | /**
22 | * Remove a file
23 | */
24 | static destroy(id) {
25 | AppDispatcher.handleViewAction({
26 | actionType: FileConstants.FILE_DESTROY,
27 | id: id
28 | });
29 | }
30 |
31 | /**
32 | * Download the file
33 | */
34 | static download(id) {
35 | AppDispatcher.handleViewAction({
36 | actionType: FileConstants.FILE_DOWNLOAD,
37 | id: id,
38 | });
39 | }
40 | }
41 |
--------------------------------------------------------------------------------
/assets/js/components/itemslist.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import * as React from "react";
4 | import { ListItem } from "./listitem";
5 |
6 | export class ItemsList extends React.Component {
7 | constructor(props) {
8 | super(props);
9 | }
10 |
11 | createItem(item, i) {
12 | return (
13 |
21 | );
22 | }
23 |
24 | getHeaderItem() {
25 | if (this.props.headerText) {
26 | return (
27 |
28 | {this.props.headerText}
29 |
30 | );
31 | }
32 |
33 | return ``;
34 | }
35 |
36 | render() {
37 | let items = this.props.items.map(this.createItem.bind(this));
38 | return (
39 |
40 | {this.getHeaderItem()}
41 | {items}
42 |
43 | );
44 | }
45 | }
46 |
--------------------------------------------------------------------------------
/lib/ephemeral_share/application.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.Application do
2 | # See https://hexdocs.pm/elixir/Application.html
3 | # for more information on OTP Applications
4 | @moduledoc false
5 |
6 | use Application
7 |
8 | def start(_type, _args) do
9 | # List all child processes to be supervised
10 | children = [
11 | # Start the Ecto repository
12 | # EphemeralShare.Repo,
13 | # Start the endpoint when the application starts
14 | EphemeralShareWeb.Endpoint
15 | # Starts a worker by calling: EphemeralShare.Worker.start_link(arg)
16 | # {EphemeralShare.Worker, arg},
17 | ]
18 |
19 | # See https://hexdocs.pm/elixir/Supervisor.html
20 | # for other strategies and supported options
21 | opts = [strategy: :one_for_one, name: EphemeralShare.Supervisor]
22 | Supervisor.start_link(children, opts)
23 | end
24 |
25 | # Tell Phoenix to update the endpoint configuration
26 | # whenever the application is updated.
27 | def config_change(changed, _new, removed) do
28 | EphemeralShareWeb.Endpoint.config_change(changed, removed)
29 | :ok
30 | end
31 | end
32 |
--------------------------------------------------------------------------------
/test/support/channel_case.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.ChannelCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | channel tests.
5 |
6 | Such tests rely on `Phoenix.ChannelTest` and also
7 | import other functionality to make it easier
8 | to build common data structures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | it cannot be async. For this reason, every test runs
12 | inside a transaction which is reset at the beginning
13 | of the test unless the test case is marked as async.
14 | """
15 |
16 | use ExUnit.CaseTemplate
17 |
18 | using do
19 | quote do
20 | # Import conveniences for testing with channels
21 | use Phoenix.ChannelTest
22 |
23 | # The default endpoint for testing
24 | @endpoint EphemeralShareWeb.Endpoint
25 | end
26 | end
27 |
28 | setup tags do
29 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EphemeralShare.Repo)
30 |
31 | unless tags[:async] do
32 | Ecto.Adapters.SQL.Sandbox.mode(EphemeralShare.Repo, {:shared, self()})
33 | end
34 |
35 | :ok
36 | end
37 | end
38 |
--------------------------------------------------------------------------------
/assets/package.json:
--------------------------------------------------------------------------------
1 | {
2 | "repository": {},
3 | "license": "MIT",
4 | "scripts": {
5 | "deploy": "webpack --mode production",
6 | "watch": "webpack --mode development --watch"
7 | },
8 | "dependencies": {
9 | "events": "^3.0.0",
10 | "flux": "^3.1.3",
11 | "phoenix": "file:../deps/phoenix",
12 | "phoenix_html": "file:../deps/phoenix_html",
13 | "react": "^16.8.6",
14 | "react-dom": "^16.8.6",
15 | "simple-peer": "^9.5.0"
16 | },
17 | "devDependencies": {
18 | "@babel/core": "^7.0.0",
19 | "@babel/plugin-proposal-class-properties": "^7.5.5",
20 | "@babel/plugin-proposal-object-rest-spread": "^7.5.5",
21 | "@babel/preset-env": "^7.0.0",
22 | "@babel/preset-react": "^7.0.0",
23 | "babel-loader": "^8.0.0",
24 | "copy-webpack-plugin": "^4.5.0",
25 | "css-loader": "^2.1.1",
26 | "mini-css-extract-plugin": "^0.4.0",
27 | "node-sass": "^4.12.0",
28 | "optimize-css-assets-webpack-plugin": "^4.0.0",
29 | "sass-loader": "^7.1.0",
30 | "styles-loader": "^1.0.2",
31 | "uglifyjs-webpack-plugin": "^1.2.4",
32 | "webpack": "4.4.0",
33 | "webpack-cli": "^2.0.10"
34 | }
35 | }
36 |
--------------------------------------------------------------------------------
/config/config.exs:
--------------------------------------------------------------------------------
1 | # This file is responsible for configuring your application
2 | # and its dependencies with the aid of the Mix.Config module.
3 | #
4 | # This configuration file is loaded before any dependency and
5 | # is restricted to this project.
6 |
7 | # General application configuration
8 | use Mix.Config
9 |
10 | # config :ephemeral_share,
11 | # ecto_repos: [EphemeralShare.Repo]
12 |
13 | # Configures the endpoint
14 | config :ephemeral_share, EphemeralShareWeb.Endpoint,
15 | url: [host: "localhost"],
16 | secret_key_base: "XijLEYX2WeHNW0Atgguqdq8nkaQ94ZHN9tx3RlFeW4D/1HSC1XYVTK1zMsrfH/26",
17 | render_errors: [view: EphemeralShareWeb.ErrorView, accepts: ~w(html json)],
18 | pubsub: [name: EphemeralShare.PubSub, adapter: Phoenix.PubSub.PG2]
19 |
20 | # Configures Elixir's Logger
21 | config :logger, :console,
22 | format: "$time $metadata[$level] $message\n",
23 | metadata: [:request_id]
24 |
25 | # Use Jason for JSON parsing in Phoenix
26 | config :phoenix, :json_library, Jason
27 |
28 | # Import environment specific config. This must remain at the bottom
29 | # of this file so it overrides the configuration defined above.
30 | import_config "#{Mix.env()}.exs"
31 |
--------------------------------------------------------------------------------
/test/support/conn_case.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.ConnCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | tests that require setting up a connection.
5 |
6 | Such tests rely on `Phoenix.ConnTest` and also
7 | import other functionality to make it easier
8 | to build common data structures and query the data layer.
9 |
10 | Finally, if the test case interacts with the database,
11 | it cannot be async. For this reason, every test runs
12 | inside a transaction which is reset at the beginning
13 | of the test unless the test case is marked as async.
14 | """
15 |
16 | use ExUnit.CaseTemplate
17 |
18 | using do
19 | quote do
20 | # Import conveniences for testing with connections
21 | use Phoenix.ConnTest
22 | alias EphemeralShareWeb.Router.Helpers, as: Routes
23 |
24 | # The default endpoint for testing
25 | @endpoint EphemeralShareWeb.Endpoint
26 | end
27 | end
28 |
29 | setup tags do
30 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EphemeralShare.Repo)
31 |
32 | unless tags[:async] do
33 | Ecto.Adapters.SQL.Sandbox.mode(EphemeralShare.Repo, {:shared, self()})
34 | end
35 |
36 | {:ok, conn: Phoenix.ConnTest.build_conn()}
37 | end
38 | end
39 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/channels/peer_socket.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PeerSocket do
2 | require Logger
3 | use Phoenix.Socket
4 |
5 | ## Channels
6 | # channel "rooms:*", TestTbd.RoomChannel
7 | channel "peer:*", EphemeralShare.PeerChannel
8 |
9 | ## Transports
10 | transport :websocket, Phoenix.Transports.WebSocket
11 | # transport :longpoll, Phoenix.Transports.LongPoll
12 |
13 | # Socket params are passed from the client and can
14 | # be used to verify and authenticate a user. After
15 | # verification, you can put default assigns into
16 | # the socket that will be set for all channels, ie
17 | #
18 | # {:ok, assign(socket, :user_id, verified_user_id)}
19 | #
20 | # To deny connection, return `:error`.
21 | def connect(_params, socket) do
22 | {:ok, socket}
23 | end
24 |
25 | # Socket id's are topics that allow you to identify all sockets for a given user:
26 | #
27 | # def id(socket), do: "users_socket:#{socket.assigns.user_id}"
28 | #
29 | # Would allow you to broadcast a "disconnect" event and terminate
30 | # all active sockets and channels for a given user:
31 | #
32 | # TestTbd.Endpoint.broadcast("users_socket:" <> user.id, "disconnect", %{})
33 | #
34 | # Returning `nil` makes this socket anonymous.
35 | def id(socket), do: nil
36 | end
37 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | # The directory Mix will write compiled artifacts to.
2 | /_build/
3 |
4 | # If you run "mix test --cover", coverage assets end up here.
5 | /cover/
6 |
7 | # The directory Mix downloads your dependencies sources to.
8 | /deps/
9 |
10 | # Where 3rd-party dependencies like ExDoc output generated docs.
11 | /doc/
12 |
13 | # Ignore .fetch files in case you like to edit your project deps locally.
14 | /.fetch
15 |
16 | # If the VM crashes, it generates a dump, let's ignore it too.
17 | erl_crash.dump
18 |
19 | # Also ignore archive artifacts (built via "mix archive.build").
20 | *.ez
21 |
22 | # Ignore package tarball (built via "mix hex.build").
23 | ephemeral_share-*.tar
24 |
25 | # If NPM crashes, it generates a log, let's ignore it too.
26 | npm-debug.log
27 |
28 | # The directory NPM downloads your dependencies sources to.
29 | /assets/node_modules/
30 | node_modules/
31 |
32 | # Since we are building assets from assets/,
33 | # we ignore priv/static. You may want to comment
34 | # this depending on your deployment strategy.
35 | /priv/static/
36 |
37 | # Files matching config/*.secret.exs pattern contain sensitive
38 | # data and you should not commit them into version control.
39 | #
40 | # Alternatively, you may comment the line below and commit the
41 | # secrets files as long as you replace their contents by environment
42 | # variables.
43 | /config/*.secret.exs
44 |
45 | .elixir_ls/
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/channels/broker_socket.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.BrokerSocket do
2 | require Logger
3 | use Phoenix.Socket
4 |
5 | ## Channels
6 | # channel "rooms:*", TestTbd.RoomChannel
7 | channel "broker:*", EphemeralShare.BrokerChannel
8 |
9 | ## Transports
10 | transport :websocket, Phoenix.Transports.WebSocket
11 | # transport :longpoll, Phoenix.Transports.LongPoll
12 |
13 | # Socket params are passed from the client and can
14 | # be used to verify and authenticate a user. After
15 | # verification, you can put default assigns into
16 | # the socket that will be set for all channels, ie
17 | #
18 | # {:ok, assign(socket, :user_id, verified_user_id)}
19 | #
20 | # To deny connection, return `:error`.
21 | def connect(_params, socket) do
22 | {:ok, assign(socket, :peer_id, generate_id())}
23 | end
24 |
25 | # Socket id's are topics that allow you to identify all sockets for a given user:
26 | #
27 | # def id(socket), do: "users_socket:#{socket.assigns.user_id}"
28 | #
29 | # Would allow you to broadcast a "disconnect" event and terminate
30 | # all active sockets and channels for a given user:
31 | #
32 | # TestTbd.Endpoint.broadcast("users_socket:" <> user.id, "disconnect", %{})
33 | #
34 | # Returning `nil` makes this socket anonymous.
35 | def id(socket), do: "brokers_socket:#{socket.assigns.peer_id}"
36 |
37 | defp generate_id(), do: UUID.uuid4()
38 | end
39 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/templates/layout/app.html.eex:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 | EphemeralShare · Phoenix Framework
8 | "/>
9 |
10 |
11 |
12 |
13 |
14 |
23 |
24 | <%= get_flash(@conn, :info) %>
25 | <%= get_flash(@conn, :error) %>
26 | <%= render @view_module, @view_template, assigns %>
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/assets/webpack.config.js:
--------------------------------------------------------------------------------
1 | const path = require("path");
2 | const glob = require("glob");
3 | const MiniCssExtractPlugin = require("mini-css-extract-plugin");
4 | const UglifyJsPlugin = require("uglifyjs-webpack-plugin");
5 | const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
6 | const CopyWebpackPlugin = require("copy-webpack-plugin");
7 |
8 | module.exports = (env, options) => ({
9 | optimization: {
10 | minimizer: [
11 | new UglifyJsPlugin({ cache: true, parallel: true, sourceMap: false }),
12 | new OptimizeCSSAssetsPlugin({})
13 | ]
14 | },
15 | entry: {
16 | "./js/app.jsx": ["./js/app.jsx"].concat(glob.sync("./vendor/**/*.js"))
17 | },
18 | output: {
19 | filename: "app.js",
20 | path: path.resolve(__dirname, "../priv/static/js")
21 | },
22 | module: {
23 | rules: [
24 | {
25 | test: /\.(js|jsx)$/,
26 | exclude: /node_modules/,
27 | use: {
28 | loader: "babel-loader"
29 | }
30 | },
31 | {
32 | test: /\.scss$/,
33 | use: ["style-loader", "css-loader", "sass-loader"]
34 | },
35 | {
36 | test: /\.css$/,
37 | use: [MiniCssExtractPlugin.loader, "css-loader"]
38 | }
39 | ]
40 | },
41 | plugins: [
42 | new MiniCssExtractPlugin({ filename: "../css/app.css" }),
43 | new CopyWebpackPlugin([{ from: "static/", to: "../" }])
44 | ],
45 | resolve: {
46 | extensions: [".js", ".jsx", ".json"]
47 | }
48 | });
49 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/endpoint.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.Endpoint do
2 | use Phoenix.Endpoint, otp_app: :ephemeral_share
3 |
4 | socket("/ws", EphemeralShare.BrokerSocket)
5 | socket("/peer", EphemeralShare.PeerSocket)
6 |
7 | # Serve at "/" the static files from "priv/static" directory.
8 | #
9 | # You should set gzip to true if you are running phx.digest
10 | # when deploying your static files in production.
11 | plug(Plug.Static,
12 | at: "/",
13 | from: :ephemeral_share,
14 | gzip: false,
15 | only: ~w(css fonts images js favicon.ico robots.txt)
16 | )
17 |
18 | # Code reloading can be explicitly enabled under the
19 | # :code_reloader configuration of your endpoint.
20 | if code_reloading? do
21 | socket("/phoenix/live_reload/socket", Phoenix.LiveReloader.Socket)
22 | plug(Phoenix.LiveReloader)
23 | plug(Phoenix.CodeReloader)
24 | end
25 |
26 | plug(Plug.RequestId)
27 | plug(Plug.Logger)
28 |
29 | plug(Plug.Parsers,
30 | parsers: [:urlencoded, :multipart, :json],
31 | pass: ["*/*"],
32 | json_decoder: Phoenix.json_library()
33 | )
34 |
35 | plug(Plug.MethodOverride)
36 | plug(Plug.Head)
37 |
38 | # The session will be stored in the cookie and signed,
39 | # this means its contents can be read but not tampered with.
40 | # Set :encryption_salt if you would also like to encrypt it.
41 | plug(Plug.Session,
42 | store: :cookie,
43 | key: "_ephemeral_share_key",
44 | signing_salt: "S62Oxyc/"
45 | )
46 |
47 | plug(EphemeralShareWeb.Router)
48 | end
49 |
--------------------------------------------------------------------------------
/test/support/data_case.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.DataCase do
2 | @moduledoc """
3 | This module defines the setup for tests requiring
4 | access to the application's data layer.
5 |
6 | You may define functions here to be used as helpers in
7 | your tests.
8 |
9 | Finally, if the test case interacts with the database,
10 | it cannot be async. For this reason, every test runs
11 | inside a transaction which is reset at the beginning
12 | of the test unless the test case is marked as async.
13 | """
14 |
15 | use ExUnit.CaseTemplate
16 |
17 | using do
18 | quote do
19 | alias EphemeralShare.Repo
20 |
21 | import Ecto
22 | import Ecto.Changeset
23 | import Ecto.Query
24 | import EphemeralShare.DataCase
25 | end
26 | end
27 |
28 | setup tags do
29 | :ok = Ecto.Adapters.SQL.Sandbox.checkout(EphemeralShare.Repo)
30 |
31 | unless tags[:async] do
32 | Ecto.Adapters.SQL.Sandbox.mode(EphemeralShare.Repo, {:shared, self()})
33 | end
34 |
35 | :ok
36 | end
37 |
38 | @doc """
39 | A helper that transforms changeset errors into a map of messages.
40 |
41 | assert {:error, changeset} = Accounts.create_user(%{password: "short"})
42 | assert "password is too short" in errors_on(changeset).password
43 | assert %{password: ["password is too short"]} = errors_on(changeset)
44 |
45 | """
46 | def errors_on(changeset) do
47 | Ecto.Changeset.traverse_errors(changeset, fn {message, opts} ->
48 | Enum.reduce(opts, message, fn {key, value}, acc ->
49 | String.replace(acc, "%{#{key}}", to_string(value))
50 | end)
51 | end)
52 | end
53 | end
54 |
--------------------------------------------------------------------------------
/test/support/model_case.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.ModelCase do
2 | @moduledoc """
3 | This module defines the test case to be used by
4 | model tests.
5 |
6 | You may define functions here to be used as helpers in
7 | your model tests. See `errors_on/2`'s definition as reference.
8 |
9 | Finally, if the test case interacts with the database,
10 | it cannot be async. For this reason, every test runs
11 | inside a transaction which is reset at the beginning
12 | of the test unless the test case is marked as async.
13 | """
14 |
15 | use ExUnit.CaseTemplate
16 |
17 | using do
18 | quote do
19 | # Alias the data repository and import query/model functions
20 | alias EphemeralShare.Repo
21 | import Ecto.Model
22 | import Ecto.Query, only: [from: 2]
23 | import EphemeralShare.ModelCase
24 | end
25 | end
26 |
27 | setup tags do
28 | unless tags[:async] do
29 | Ecto.Adapters.SQL.restart_test_transaction(EphemeralShare.Repo, [])
30 | end
31 |
32 | :ok
33 | end
34 |
35 | @doc """
36 | Helper for returning list of errors in model when passed certain data.
37 |
38 | ## Examples
39 |
40 | Given a User model that has validation for the presence of a value for the
41 | `:name` field and validation that `:password` is "safe":
42 |
43 | iex> errors_on(%User{}, password: "password")
44 | [{:password, "is unsafe"}, {:name, "is blank"}]
45 |
46 | You would then write your assertion like:
47 |
48 | assert {:password, "is unsafe"} in errors_on(%User{}, password: "password")
49 | """
50 | def errors_on(model, data) do
51 | model.__struct__.changeset(model, data).errors
52 | end
53 | end
54 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/views/error_helpers.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb.ErrorHelpers do
2 | @moduledoc """
3 | Conveniences for translating and building error messages.
4 | """
5 |
6 | use Phoenix.HTML
7 |
8 | @doc """
9 | Generates tag for inlined form input errors.
10 | """
11 | def error_tag(form, field) do
12 | Enum.map(Keyword.get_values(form.errors, field), fn error ->
13 | content_tag(:span, translate_error(error), class: "help-block")
14 | end)
15 | end
16 |
17 | @doc """
18 | Translates an error message using gettext.
19 | """
20 | def translate_error({msg, opts}) do
21 | # When using gettext, we typically pass the strings we want
22 | # to translate as a static argument:
23 | #
24 | # # Translate "is invalid" in the "errors" domain
25 | # dgettext("errors", "is invalid")
26 | #
27 | # # Translate the number of files with plural rules
28 | # dngettext("errors", "1 file", "%{count} files", count)
29 | #
30 | # Because the error messages we show in our forms and APIs
31 | # are defined inside Ecto, we need to translate them dynamically.
32 | # This requires us to call the Gettext module passing our gettext
33 | # backend as first argument.
34 | #
35 | # Note we use the "errors" domain, which means translations
36 | # should be written to the errors.po file. The :count option is
37 | # set by Ecto and indicates we should also apply plural rules.
38 | if count = opts[:count] do
39 | Gettext.dngettext(EphemeralShareWeb.Gettext, "errors", msg, msg, count, opts)
40 | else
41 | Gettext.dgettext(EphemeralShareWeb.Gettext, "errors", msg, opts)
42 | end
43 | end
44 | end
45 |
--------------------------------------------------------------------------------
/mix.exs:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.MixProject do
2 | use Mix.Project
3 |
4 | def project do
5 | [
6 | app: :ephemeral_share,
7 | version: "0.1.0",
8 | elixir: "~> 1.5",
9 | elixirc_paths: elixirc_paths(Mix.env()),
10 | compilers: [:phoenix, :gettext] ++ Mix.compilers(),
11 | start_permanent: Mix.env() == :prod,
12 | aliases: aliases(),
13 | deps: deps()
14 | ]
15 | end
16 |
17 | # Configuration for the OTP application.
18 | #
19 | # Type `mix help compile.app` for more information.
20 | def application do
21 | [
22 | mod: {EphemeralShare.Application, []},
23 | extra_applications: [:logger, :runtime_tools]
24 | ]
25 | end
26 |
27 | # Specifies which paths to compile per environment.
28 | defp elixirc_paths(:test), do: ["lib", "test/support"]
29 | defp elixirc_paths(_), do: ["lib"]
30 |
31 | # Specifies your project dependencies.
32 | #
33 | # Type `mix help deps` for examples and options.
34 | defp deps do
35 | [
36 | {:phoenix, "~> 1.4.3"},
37 | {:phoenix_pubsub, "~> 1.1"},
38 | {:phoenix_ecto, "~> 4.0"},
39 | {:ecto_sql, "~> 3.0"},
40 | {:postgrex, ">= 0.0.0"},
41 | {:phoenix_html, "~> 2.11"},
42 | {:phoenix_live_reload, "~> 1.2", only: :dev},
43 | {:gettext, "~> 0.11"},
44 | {:jason, "~> 1.0"},
45 | {:plug_cowboy, "~> 2.0"},
46 | {:uuid, "~> 1.0"}
47 | ]
48 | end
49 |
50 | # Aliases are shortcuts or tasks specific to the current project.
51 | # For example, to create, migrate and run the seeds file at once:
52 | #
53 | # $ mix ecto.setup
54 | #
55 | # See the documentation for `Mix` for more info on aliases.
56 | defp aliases do
57 | [
58 | "ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
59 | "ecto.reset": ["ecto.drop", "ecto.setup"],
60 | test: ["ecto.create --quiet", "ecto.migrate", "test"]
61 | ]
62 | end
63 | end
64 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShareWeb do
2 | @moduledoc """
3 | The entrypoint for defining your web interface, such
4 | as controllers, views, channels and so on.
5 |
6 | This can be used in your application as:
7 |
8 | use EphemeralShareWeb, :controller
9 | use EphemeralShareWeb, :view
10 |
11 | The definitions below will be executed for every view,
12 | controller, etc, so keep them short and clean, focused
13 | on imports, uses and aliases.
14 |
15 | Do NOT define functions inside the quoted expressions
16 | below. Instead, define any helper function in modules
17 | and import those modules here.
18 | """
19 |
20 | def controller do
21 | quote do
22 | use Phoenix.Controller, namespace: EphemeralShareWeb
23 |
24 | import Plug.Conn
25 | import EphemeralShareWeb.Gettext
26 | alias EphemeralShareWeb.Router.Helpers, as: Routes
27 | end
28 | end
29 |
30 | def view do
31 | quote do
32 | use Phoenix.View,
33 | root: "lib/ephemeral_share_web/templates",
34 | namespace: EphemeralShareWeb
35 |
36 | # Import convenience functions from controllers
37 | import Phoenix.Controller, only: [get_flash: 1, get_flash: 2, view_module: 1]
38 |
39 | # Use all HTML functionality (forms, tags, etc)
40 | use Phoenix.HTML
41 |
42 | import EphemeralShareWeb.ErrorHelpers
43 | import EphemeralShareWeb.Gettext
44 | alias EphemeralShareWeb.Router.Helpers, as: Routes
45 | end
46 | end
47 |
48 | def router do
49 | quote do
50 | use Phoenix.Router
51 | import Plug.Conn
52 | import Phoenix.Controller
53 | end
54 | end
55 |
56 | def channel do
57 | quote do
58 | use Phoenix.Channel
59 | import EphemeralShareWeb.Gettext
60 | end
61 | end
62 |
63 | @doc """
64 | When used, dispatch to the appropriate controller/view/etc.
65 | """
66 | defmacro __using__(which) when is_atom(which) do
67 | apply(__MODULE__, which, [])
68 | end
69 | end
70 |
--------------------------------------------------------------------------------
/assets/js/components/filehandler.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import * as React from "react";
4 |
5 | export class FileHandler extends React.Component {
6 | constructor(props) {
7 | super(props);
8 | }
9 |
10 | dragOver(event) {
11 | event.stopPropagation();
12 | event.preventDefault();
13 | event.dataTransfer.dropEffect = "copy";
14 | }
15 |
16 | drop(event) {
17 | event.stopPropagation();
18 | event.preventDefault();
19 |
20 | let files = event.dataTransfer.files;
21 |
22 | if (this.props.onFileSelected) {
23 | this.props.onFileSelected(files);
24 | }
25 | }
26 |
27 | /* Render */
28 | render() {
29 | let featureIcon = {
30 | marginRight: "4px"
31 | };
32 |
33 | return (
34 |
41 |
42 |
43 |
44 |
45 | lock
46 |
47 | Secure
48 |
49 |
50 |
51 | visibility_off
52 |
53 | Private
54 |
55 |
56 |
57 | swap_horiz
58 |
59 | Direct from you to receiver
60 |
61 |
62 |
63 |
64 |
65 | How to share:
66 |
67 | Drag & Drop file in this box
68 | Share the link with the receipient
69 | Wait for them to download
70 |
71 |
72 |
Drop file here
73 |
74 | );
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/config/dev.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # For development, we disable any cache and enable
4 | # debugging and code reloading.
5 | #
6 | # The watchers configuration can be used to run external
7 | # watchers to your application. For example, we use it
8 | # with webpack to recompile .js and .css sources.
9 | config :ephemeral_share, EphemeralShareWeb.Endpoint,
10 | http: [port: 4000],
11 | debug_errors: true,
12 | code_reloader: true,
13 | check_origin: false,
14 | watchers: [
15 | node: [
16 | "node_modules/webpack/bin/webpack.js",
17 | "--mode",
18 | "development",
19 | "--watch-stdin",
20 | cd: Path.expand("../assets", __DIR__)
21 | ]
22 | ]
23 |
24 | # ## SSL Support
25 | #
26 | # In order to use HTTPS in development, a self-signed
27 | # certificate can be generated by running the following
28 | # Mix task:
29 | #
30 | # mix phx.gen.cert
31 | #
32 | # Note that this task requires Erlang/OTP 20 or later.
33 | # Run `mix help phx.gen.cert` for more information.
34 | #
35 | # The `http:` config above can be replaced with:
36 | #
37 | # https: [
38 | # port: 4001,
39 | # cipher_suite: :strong,
40 | # keyfile: "priv/cert/selfsigned_key.pem",
41 | # certfile: "priv/cert/selfsigned.pem"
42 | # ],
43 | #
44 | # If desired, both `http:` and `https:` keys can be
45 | # configured to run both http and https servers on
46 | # different ports.
47 |
48 | # Watch static and templates for browser reloading.
49 | config :ephemeral_share, EphemeralShareWeb.Endpoint,
50 | live_reload: [
51 | patterns: [
52 | ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
53 | ~r"priv/gettext/.*(po)$",
54 | ~r"lib/ephemeral_share_web/{live,views}/.*(ex)$",
55 | ~r"lib/ephemeral_share_web/templates/.*(eex)$"
56 | ]
57 | ]
58 |
59 | # Do not include metadata nor timestamps in development logs
60 | config :logger, :console, format: "[$level] $message\n"
61 |
62 | # Set a higher stacktrace during development. Avoid configuring such
63 | # in production as building large stacktraces may be expensive.
64 | config :phoenix, :stacktrace_depth, 20
65 |
66 | # Initialize plugs at runtime for faster development compilation
67 | config :phoenix, :plug_init_mode, :runtime
68 |
69 | # Configure your database
70 | # config :ephemeral_share, EphemeralShare.Repo,
71 | # username: "postgres",
72 | # password: "postgres",
73 | # database: "ephemeral_share_dev",
74 | # hostname: "localhost",
75 | # pool_size: 10
76 |
--------------------------------------------------------------------------------
/assets/js/components/peerconnectionstatus.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import * as React from "react";
4 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
5 | import { PeerCommunicationProtocol } from "../lib/peercommunication";
6 |
7 | const STATUS_CONNECTING = 0;
8 | const STATUS_CONNECTED = 1;
9 | const STATUS_DOES_NOT_EXIST = 2;
10 |
11 | export class PeerConnectionStatus extends React.Component {
12 | constructor(props) {
13 | super(props);
14 | this.state = { status: STATUS_CONNECTING };
15 | }
16 |
17 | componentWillMount() {
18 | PeerCommunicationProtocol.instance().addEventListener(
19 | PeerCommunicationConstants.PEER_CONNECTED,
20 | this.onConnectedToPeer.bind(this)
21 | );
22 | PeerCommunicationProtocol.instance().addEventListener(
23 | PeerCommunicationConstants.PEER_DOES_NOT_EXIST,
24 | this.onPeerDoesNotExist.bind(this)
25 | );
26 | }
27 |
28 | componentWillUnmount() {
29 | PeerCommunicationProtocol.instance().removeEventListener(
30 | PeerCommunicationConstants.PEER_CONNECTED,
31 | this.onConnectedToPeer.bind(this)
32 | );
33 | PeerCommunicationProtocol.instance().removeEventListener(
34 | PeerCommunicationConstants.PEER_DOES_NOT_EXIST,
35 | this.onPeerDoesNotExist.bind(this)
36 | );
37 | }
38 |
39 | onPeerDoesNotExist(m) {
40 | this.setState({ status: STATUS_DOES_NOT_EXIST });
41 | }
42 |
43 | onConnectedToPeer(e) {
44 | this.setState({ status: STATUS_CONNECTED });
45 | }
46 |
47 | getStatusMessage() {
48 | if (this.state.status === STATUS_CONNECTING) {
49 | return "Connecting ...";
50 | } else if (this.state.status === STATUS_CONNECTED) {
51 | return "Connected";
52 | } else if (this.state.status === STATUS_DOES_NOT_EXIST) {
53 | return "Error: Incorrect share link. Please ask the user to send you the share link again.";
54 | }
55 |
56 | return "Please refresh and make sure the share link is correct";
57 | }
58 |
59 | getWorkingBar() {
60 | if (this.state.status == STATUS_CONNECTING) {
61 | return (
62 |
65 | );
66 | }
67 |
68 | return "";
69 | }
70 |
71 | render() {
72 | return (
73 |
74 |
75 |
{this.getStatusMessage()}
76 | {this.getWorkingBar()}
77 |
78 |
79 | );
80 | }
81 | }
82 |
--------------------------------------------------------------------------------
/assets/js/app.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import { Socket } from "phoenix";
4 | import * as React from "react";
5 | import * as ReactDOM from "react-dom";
6 |
7 | import { PeerCommunicationProtocol } from "./lib/peercommunication";
8 | import { FileInfoStore } from "./stores/fileinfostore";
9 | import { FileShareApp } from "./components/filelistitem";
10 | import { FileTransferManager } from "./lib/filetransfermanager";
11 | import { ErrorBanner } from "./components/errorbanner";
12 |
13 | class App {
14 | static init() {
15 | let webRTCSupported =
16 | window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
17 | let webSocketSupported = "WebSocket" in window;
18 | let isAppSupported = webRTCSupported && webSocketSupported;
19 |
20 | let getParams = App.getURLParams();
21 | let isInitiator = getParams.peer_id === undefined;
22 |
23 | let peer_id = null;
24 | if (getParams.peer_id) {
25 | peer_id = getParams.peer_id;
26 | }
27 | let hasConnected = false;
28 |
29 | PeerCommunicationProtocol.initialize(
30 | isInitiator,
31 | null,
32 | pcp => {
33 | if (peer_id && !hasConnected) {
34 | hasConnected = true;
35 | pcp.connect(peer_id);
36 | }
37 | },
38 | rtc => {
39 | console.log("Peer Connected", rtc);
40 | }
41 | );
42 |
43 | FileInfoStore.initialize(isInitiator);
44 | FileTransferManager.initialize(peer_id);
45 |
46 | // After the store dependency FileTransferManager is created we call the initialize on store instance
47 | // TODO: Have a better way instead of a circular dependency
48 | FileInfoStore.instance().initialize();
49 |
50 | if (isAppSupported) {
51 | ReactDOM.render(
52 | ,
53 | document.getElementById("react-container")
54 | );
55 | } else {
56 | ReactDOM.render(
57 | ,
58 | document.getElementById("react-container")
59 | );
60 | }
61 | }
62 |
63 | static getURLParams() {
64 | var queryDict = {};
65 | location.search
66 | .substr(1)
67 | .split("&")
68 | .forEach(function(item) {
69 | queryDict[item.split("=")[0]] = item.split("=")[1];
70 | });
71 | return queryDict;
72 | }
73 | }
74 |
75 | App.init();
76 | export default App;
77 |
--------------------------------------------------------------------------------
/priv/gettext/en/LC_MESSAGES/errors.po:
--------------------------------------------------------------------------------
1 | ## `msgid`s in this file come from POT (.pot) files.
2 | ##
3 | ## Do not add, change, or remove `msgid`s manually here as
4 | ## they're tied to the ones in the corresponding POT file
5 | ## (with the same domain).
6 | ##
7 | ## Use `mix gettext.extract --merge` or `mix gettext.merge`
8 | ## to merge POT files into PO files.
9 | msgid ""
10 | msgstr ""
11 | "Language: en\n"
12 |
13 | ## From Ecto.Changeset.cast/4
14 | msgid "can't be blank"
15 | msgstr ""
16 |
17 | ## From Ecto.Changeset.unique_constraint/3
18 | msgid "has already been taken"
19 | msgstr ""
20 |
21 | ## From Ecto.Changeset.put_change/3
22 | msgid "is invalid"
23 | msgstr ""
24 |
25 | ## From Ecto.Changeset.validate_acceptance/3
26 | msgid "must be accepted"
27 | msgstr ""
28 |
29 | ## From Ecto.Changeset.validate_format/3
30 | msgid "has invalid format"
31 | msgstr ""
32 |
33 | ## From Ecto.Changeset.validate_subset/3
34 | msgid "has an invalid entry"
35 | msgstr ""
36 |
37 | ## From Ecto.Changeset.validate_exclusion/3
38 | msgid "is reserved"
39 | msgstr ""
40 |
41 | ## From Ecto.Changeset.validate_confirmation/3
42 | msgid "does not match confirmation"
43 | msgstr ""
44 |
45 | ## From Ecto.Changeset.no_assoc_constraint/3
46 | msgid "is still associated with this entry"
47 | msgstr ""
48 |
49 | msgid "are still associated with this entry"
50 | msgstr ""
51 |
52 | ## From Ecto.Changeset.validate_length/3
53 | msgid "should be %{count} character(s)"
54 | msgid_plural "should be %{count} character(s)"
55 | msgstr[0] ""
56 | msgstr[1] ""
57 |
58 | msgid "should have %{count} item(s)"
59 | msgid_plural "should have %{count} item(s)"
60 | msgstr[0] ""
61 | msgstr[1] ""
62 |
63 | msgid "should be at least %{count} character(s)"
64 | msgid_plural "should be at least %{count} character(s)"
65 | msgstr[0] ""
66 | msgstr[1] ""
67 |
68 | msgid "should have at least %{count} item(s)"
69 | msgid_plural "should have at least %{count} item(s)"
70 | msgstr[0] ""
71 | msgstr[1] ""
72 |
73 | msgid "should be at most %{count} character(s)"
74 | msgid_plural "should be at most %{count} character(s)"
75 | msgstr[0] ""
76 | msgstr[1] ""
77 |
78 | msgid "should have at most %{count} item(s)"
79 | msgid_plural "should have at most %{count} item(s)"
80 | msgstr[0] ""
81 | msgstr[1] ""
82 |
83 | ## From Ecto.Changeset.validate_number/3
84 | msgid "must be less than %{number}"
85 | msgstr ""
86 |
87 | msgid "must be greater than %{number}"
88 | msgstr ""
89 |
90 | msgid "must be less than or equal to %{number}"
91 | msgstr ""
92 |
93 | msgid "must be greater than or equal to %{number}"
94 | msgstr ""
95 |
96 | msgid "must be equal to %{number}"
97 | msgstr ""
98 |
--------------------------------------------------------------------------------
/priv/gettext/errors.pot:
--------------------------------------------------------------------------------
1 | ## This is a PO Template file.
2 | ##
3 | ## `msgid`s here are often extracted from source code.
4 | ## Add new translations manually only if they're dynamic
5 | ## translations that can't be statically extracted.
6 | ##
7 | ## Run `mix gettext.extract` to bring this file up to
8 | ## date. Leave `msgstr`s empty as changing them here has no
9 | ## effect: edit them in PO (`.po`) files instead.
10 |
11 | ## From Ecto.Changeset.cast/4
12 | msgid "can't be blank"
13 | msgstr ""
14 |
15 | ## From Ecto.Changeset.unique_constraint/3
16 | msgid "has already been taken"
17 | msgstr ""
18 |
19 | ## From Ecto.Changeset.put_change/3
20 | msgid "is invalid"
21 | msgstr ""
22 |
23 | ## From Ecto.Changeset.validate_acceptance/3
24 | msgid "must be accepted"
25 | msgstr ""
26 |
27 | ## From Ecto.Changeset.validate_format/3
28 | msgid "has invalid format"
29 | msgstr ""
30 |
31 | ## From Ecto.Changeset.validate_subset/3
32 | msgid "has an invalid entry"
33 | msgstr ""
34 |
35 | ## From Ecto.Changeset.validate_exclusion/3
36 | msgid "is reserved"
37 | msgstr ""
38 |
39 | ## From Ecto.Changeset.validate_confirmation/3
40 | msgid "does not match confirmation"
41 | msgstr ""
42 |
43 | ## From Ecto.Changeset.no_assoc_constraint/3
44 | msgid "is still associated with this entry"
45 | msgstr ""
46 |
47 | msgid "are still associated with this entry"
48 | msgstr ""
49 |
50 | ## From Ecto.Changeset.validate_length/3
51 | msgid "should be %{count} character(s)"
52 | msgid_plural "should be %{count} character(s)"
53 | msgstr[0] ""
54 | msgstr[1] ""
55 |
56 | msgid "should have %{count} item(s)"
57 | msgid_plural "should have %{count} item(s)"
58 | msgstr[0] ""
59 | msgstr[1] ""
60 |
61 | msgid "should be at least %{count} character(s)"
62 | msgid_plural "should be at least %{count} character(s)"
63 | msgstr[0] ""
64 | msgstr[1] ""
65 |
66 | msgid "should have at least %{count} item(s)"
67 | msgid_plural "should have at least %{count} item(s)"
68 | msgstr[0] ""
69 | msgstr[1] ""
70 |
71 | msgid "should be at most %{count} character(s)"
72 | msgid_plural "should be at most %{count} character(s)"
73 | msgstr[0] ""
74 | msgstr[1] ""
75 |
76 | msgid "should have at most %{count} item(s)"
77 | msgid_plural "should have at most %{count} item(s)"
78 | msgstr[0] ""
79 | msgstr[1] ""
80 |
81 | ## From Ecto.Changeset.validate_number/3
82 | msgid "must be less than %{number}"
83 | msgstr ""
84 |
85 | msgid "must be greater than %{number}"
86 | msgstr ""
87 |
88 | msgid "must be less than or equal to %{number}"
89 | msgstr ""
90 |
91 | msgid "must be greater than or equal to %{number}"
92 | msgstr ""
93 |
94 | msgid "must be equal to %{number}"
95 | msgstr ""
96 |
--------------------------------------------------------------------------------
/config/prod.exs:
--------------------------------------------------------------------------------
1 | use Mix.Config
2 |
3 | # For production, don't forget to configure the url host
4 | # to something meaningful, Phoenix uses this information
5 | # when generating URLs.
6 | #
7 | # Note we also include the path to a cache manifest
8 | # containing the digested version of static files. This
9 | # manifest is generated by the `mix phx.digest` task,
10 | # which you should run after static files are built and
11 | # before starting your production server.
12 | config :ephemeral_share, EphemeralShareWeb.Endpoint,
13 | http: [:inet6, port: System.get_env("PORT") || 4000],
14 | url: [host: "localhost", port: 80],
15 | cache_static_manifest: "priv/static/cache_manifest.json"
16 |
17 | # Do not print debug messages in production
18 | config :logger, level: :info
19 |
20 | # ## SSL Support
21 | #
22 | # To get SSL working, you will need to add the `https` key
23 | # to the previous section and set your `:url` port to 443:
24 | #
25 | # config :ephemeral_share, EphemeralShareWeb.Endpoint,
26 | # ...
27 | # url: [host: "example.com", port: 443],
28 | # https: [
29 | # :inet6,
30 | # port: 443,
31 | # cipher_suite: :strong,
32 | # keyfile: System.get_env("SOME_APP_SSL_KEY_PATH"),
33 | # certfile: System.get_env("SOME_APP_SSL_CERT_PATH")
34 | # ]
35 | #
36 | # The `cipher_suite` is set to `:strong` to support only the
37 | # latest and more secure SSL ciphers. This means old browsers
38 | # and clients may not be supported. You can set it to
39 | # `:compatible` for wider support.
40 | #
41 | # `:keyfile` and `:certfile` expect an absolute path to the key
42 | # and cert in disk or a relative path inside priv, for example
43 | # "priv/ssl/server.key". For all supported SSL configuration
44 | # options, see https://hexdocs.pm/plug/Plug.SSL.html#configure/1
45 | #
46 | # We also recommend setting `force_ssl` in your endpoint, ensuring
47 | # no data is ever sent via http, always redirecting to https:
48 | #
49 | # config :ephemeral_share, EphemeralShareWeb.Endpoint,
50 | # force_ssl: [hsts: true]
51 | #
52 | # Check `Plug.SSL` for all available options in `force_ssl`.
53 |
54 | # ## Using releases (distillery)
55 | #
56 | # If you are doing OTP releases, you need to instruct Phoenix
57 | # to start the server for all endpoints:
58 | #
59 | # config :phoenix, :serve_endpoints, true
60 | #
61 | # Alternatively, you can configure exactly which server to
62 | # start per endpoint:
63 | #
64 | # config :ephemeral_share, EphemeralShareWeb.Endpoint, server: true
65 | #
66 | # Note you can't rely on `System.get_env/1` when using releases.
67 | # See the releases documentation accordingly.
68 |
69 | # Finally import the config/prod.secret.exs which should be versioned
70 | # separately.
71 | import_config "prod.secret.exs"
72 |
--------------------------------------------------------------------------------
/lib/ephemeral_share_web/channels/peer_channel.ex:
--------------------------------------------------------------------------------
1 | defmodule EphemeralShare.PeerChannel do
2 | use Phoenix.Channel
3 |
4 | require Logger
5 | alias EphemeralShare.PeerManager
6 |
7 | @doc """
8 | Join the topic `peer:` and add the peer in the peer manager
9 | """
10 | def join("peer:" <> peer_id, _auth_msg, socket) do
11 | Logger.debug("[PeerChannel] join peer:#{peer_id}")
12 |
13 | case valid_uuid?(peer_id) do
14 | true ->
15 | PeerManager.add_peer(peer_id)
16 | {:ok, assign(socket, :peer_id, peer_id)}
17 |
18 | false ->
19 | {:error, %{reason: "Invalid peer id"}}
20 | end
21 | end
22 |
23 | def join(_, _auth_msg, socket) do
24 | {:error, %{reason: "Invalid subchannel"}}
25 | end
26 |
27 | @doc """
28 | The socket connection got terminated, should remove the peer information from the
29 | peer manager
30 | """
31 | def terminate(_msg, socket) do
32 | PeerManager.remove_peer(socket.assigns[:peer_id])
33 | {:shutdown, :left}
34 | end
35 |
36 | # Topic handlers
37 |
38 | @doc """
39 | Send a request to connect with a peer to the peer with which the `sender_id` peer
40 | wants to connect.
41 | """
42 | def handle_in("connect", %{"peer_id" => peer_id, "sender_id" => sender_id}, socket) do
43 | Logger.debug("[PeerChannel] Connect Request #{peer_id}, #{sender_id}")
44 |
45 | case send_to_peer(peer_id, "peer_connect", %{peer_id: sender_id}) do
46 | {:error, _} ->
47 | Logger.error("[PeerChannel] Error sending to peer #{peer_id}")
48 | push(socket, "error_connect", %{id: peer_id})
49 |
50 | :ok ->
51 | Logger.debug("[PeerChannel] Sent peer connect request to #{peer_id} from #{sender_id}")
52 |
53 | x ->
54 | Logger.error("[PeerChannel] Got unexpected peer connect #{x}")
55 | end
56 |
57 | {:noreply, socket}
58 | end
59 |
60 | @doc """
61 | Pass the offer recieved from the initiator to the peer who wants to connect
62 | """
63 | def handle_in(
64 | "offer",
65 | %{"offer" => offer, "peer_id" => peer_id, "sender_id" => sender_id},
66 | socket
67 | ) do
68 | Logger.debug("Offer Request #{inspect(offer)}, #{peer_id}, #{sender_id}")
69 | send_to_peer(peer_id, "offer", %{offer: offer, peer_id: sender_id})
70 | {:noreply, socket}
71 | end
72 |
73 | @doc """
74 | Pass the answer received from the connecting peer to the initiator
75 | """
76 | def handle_in(
77 | "answer",
78 | %{"answer" => answer, "peer_id" => peer_id, "sender_id" => sender_id},
79 | socket
80 | ) do
81 | Logger.debug("Answer Request #{inspect(answer)}, #{peer_id}, #{sender_id}")
82 | send_to_peer(peer_id, "answer", %{answer: answer, peer_id: sender_id})
83 | {:noreply, socket}
84 | end
85 |
86 | defp send_to_peer(peer_id, event, msg) do
87 | case PeerManager.exists?(peer_id) do
88 | false ->
89 | Logger.error("[PeerChannel] Peer #{peer_id} does not exist")
90 | {:error, %{reason: "No such client exists"}}
91 |
92 | true ->
93 | Logger.info("[PeerChannel] Broadcasting to peer #{peer_id}")
94 | EphemeralShareWeb.Endpoint.broadcast("peer:" <> peer_id, event, msg)
95 | end
96 | end
97 |
98 | defp valid_uuid?(id) do
99 | try do
100 | UUID.info!(id)
101 | rescue
102 | e in ArgumentError -> false
103 | end
104 |
105 | true
106 | end
107 | end
108 |
--------------------------------------------------------------------------------
/assets/js/components/listitem.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import * as React from "react";
4 | export class ListItem extends React.Component {
5 | constructor(props) {
6 | super(props);
7 | this.state = { downloadInProgress: false };
8 | }
9 |
10 | onDelete(event) {
11 | if (this.props.onDelete) {
12 | this.props.onDelete(this.props.dataNum);
13 | }
14 | }
15 |
16 | onSave(event) {
17 | console.log(`Download ${this.props.data.name}`);
18 | if (this.props.onSave) {
19 | this.setState({ downloadInProgress: true });
20 | this.props.onSave(this.props.dataNum);
21 | }
22 | }
23 |
24 | sizeInMb(bytes) {
25 | let mb = bytes / (1000 * 1000);
26 | return Math.round(mb * 100) / 100;
27 | }
28 |
29 | isSaveDisabled() {
30 | if (this.props.disableSave !== undefined) {
31 | return this.props.disableSave;
32 | }
33 |
34 | return false;
35 | }
36 |
37 | getFileAction() {
38 | if (this.isSaveDisabled()) {
39 | return (
40 |
45 | delete
46 |
47 | );
48 | } else if (this.props.data.downloadUrl) {
49 | return (
50 |
55 | file_download
56 |
57 | );
58 | } else if (!this.state.downloadInProgress) {
59 | return (
60 |
65 | cloud_download
66 |
67 | );
68 | }
69 |
70 | return ``;
71 | }
72 |
73 | getFileDownloadProgress() {
74 | if (this.props.data.downloadProgress && !this.props.data.downloadUrl) {
75 | return (
76 |
82 | );
83 | } else {
84 | return ``;
85 | }
86 | }
87 |
88 | getFileUploadProgress() {
89 | if (this.props.data.uploadProgress) {
90 | return Object.keys(this.props.data.uploadProgress).map(key => {
91 | return (
92 |
100 | );
101 | });
102 | } else {
103 | return ``;
104 | }
105 | }
106 |
107 | render() {
108 | let fileItem = {
109 | marginTop: "2px",
110 | marginBottom: "3px"
111 | };
112 |
113 | return (
114 |
115 | insert_drive_file
116 | {this.props.data.name}
117 | Size: {this.sizeInMb(this.props.data.size)} MB
118 | {this.getFileDownloadProgress()}
119 | {this.getFileUploadProgress()}
120 | {this.getFileAction()}
121 |
122 | );
123 | }
124 | }
125 |
--------------------------------------------------------------------------------
/assets/js/components/filelistitem.jsx:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import * as React from "react";
4 | import { ItemsList } from "./itemslist";
5 | import { FileHandler } from "./filehandler";
6 | import { PeerConnectionStatus } from "./peerconnectionstatus";
7 | import { FileInfoStore } from "../stores/fileinfostore";
8 | import { FileActions } from "../actions/fileactions";
9 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
10 | import { PeerCommunicationProtocol } from "../lib/peercommunication";
11 |
12 | export class FileShareApp extends React.Component {
13 | constructor(props) {
14 | super(props);
15 | this.state = { items: [], shareURL: null };
16 | }
17 |
18 | componentWillMount() {
19 | FileInfoStore.instance().addChangeListener(
20 | this.onStoreFileUpdated.bind(this)
21 | );
22 | PeerCommunicationProtocol.instance().addEventListener(
23 | PeerCommunicationConstants.CONNECTED,
24 | this.onConnectedToServer.bind(this)
25 | );
26 |
27 | let id = PeerCommunicationProtocol.instance().getId();
28 | this.createShareURLAndUpdateIfValid(id);
29 | }
30 |
31 | componentWillUnmount() {
32 | FileInfoStore.instance().removeChangeListener(
33 | this.onStoreFileUpdated.bind(this)
34 | );
35 | PeerCommunicationProtocol.instance().removeEventListener(
36 | PeerCommunicationConstants.CONNECTED,
37 | this.onConnectedToServer.bind(this)
38 | );
39 | }
40 |
41 | createShareURLAndUpdateIfValid(id) {
42 | if (id !== null) {
43 | let shareURL = `${location.protocol}//${location.host}/?peer_id=${id}`;
44 | this.setState({ shareURL: shareURL });
45 | }
46 | }
47 |
48 | onConnectedToServer(p) {
49 | let id = PeerCommunicationProtocol.instance().getId();
50 | this.createShareURLAndUpdateIfValid(id);
51 | }
52 |
53 | onStoreFileUpdated(files) {
54 | this.setState({ items: files });
55 | }
56 |
57 | onFileDeleted(index) {
58 | FileActions.destroy(index);
59 | }
60 |
61 | onFileDownload(index) {
62 | console.log(`Download ${index}`);
63 | FileActions.download(index);
64 | }
65 |
66 | onFileAdded(files) {
67 | for (let i = 0; i < files.length; i++) {
68 | FileActions.create(files[i]);
69 | }
70 | }
71 |
72 | fileDropDisabled() {
73 | if (this.props.disableDrop) {
74 | return this.props.disableDrop;
75 | }
76 |
77 | return false;
78 | }
79 |
80 | onShareURLFocus(event) {
81 | let target = event.target;
82 | setTimeout(() => {
83 | target.select();
84 | }, 0);
85 | }
86 |
87 | getShareLocation() {
88 | return (
89 |
93 |
94 |
100 |
104 |
Share the following link
105 |
112 |
113 |
114 |
115 | );
116 | }
117 |
118 | getListHeaderText() {
119 | if (this.fileDropDisabled()) {
120 | return "Files available";
121 | }
122 |
123 | return "Files added";
124 | }
125 | render() {
126 | return (
127 |
128 |
129 | {this.fileDropDisabled() ?
: ``}
130 | {this.getShareLocation()}
131 |
139 |
140 |
141 |
154 |
155 | );
156 | }
157 | }
158 |
--------------------------------------------------------------------------------
/mix.lock:
--------------------------------------------------------------------------------
1 | %{
2 | "connection": {:hex, :connection, "1.0.4", "a1cae72211f0eef17705aaededacac3eb30e6625b04a6117c1b2db6ace7d5976", [:mix], [], "hexpm"},
3 | "cowboy": {:hex, :cowboy, "2.6.3", "99aa50e94e685557cad82e704457336a453d4abcb77839ad22dbe71f311fcc06", [:rebar3], [{:cowlib, "~> 2.7.3", [hex: :cowlib, repo: "hexpm", optional: false]}, {:ranch, "~> 1.7.1", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm"},
4 | "cowlib": {:hex, :cowlib, "2.7.3", "a7ffcd0917e6d50b4d5fb28e9e2085a0ceb3c97dea310505f7460ff5ed764ce9", [:rebar3], [], "hexpm"},
5 | "db_connection": {:hex, :db_connection, "2.1.1", "a51e8a2ee54ef2ae6ec41a668c85787ed40cb8944928c191280fe34c15b76ae5", [:mix], [{:connection, "~> 1.0.2", [hex: :connection, repo: "hexpm", optional: false]}], "hexpm"},
6 | "decimal": {:hex, :decimal, "1.8.0", "ca462e0d885f09a1c5a342dbd7c1dcf27ea63548c65a65e67334f4b61803822e", [:mix], [], "hexpm"},
7 | "ecto": {:hex, :ecto, "3.1.7", "fa21d06ef56cdc2fdaa62574e8c3ba34a2751d44ea34c30bc65f0728421043e5", [:mix], [{:decimal, "~> 1.6", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
8 | "ecto_sql": {:hex, :ecto_sql, "3.1.6", "1e80e30d16138a729c717f73dcb938590bcdb3a4502f3012414d0cbb261045d8", [:mix], [{:db_connection, "~> 2.0", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.1.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:mariaex, "~> 0.9.1", [hex: :mariaex, repo: "hexpm", optional: true]}, {:myxql, "~> 0.2.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.14.0 or ~> 0.15.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
9 | "file_system": {:hex, :file_system, "0.2.7", "e6f7f155970975789f26e77b8b8d8ab084c59844d8ecfaf58cbda31c494d14aa", [:mix], [], "hexpm"},
10 | "gettext": {:hex, :gettext, "0.17.0", "abe21542c831887a2b16f4c94556db9c421ab301aee417b7c4fbde7fbdbe01ec", [:mix], [], "hexpm"},
11 | "jason": {:hex, :jason, "1.1.2", "b03dedea67a99223a2eaf9f1264ce37154564de899fd3d8b9a21b1a6fd64afe7", [:mix], [{:decimal, "~> 1.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm"},
12 | "mime": {:hex, :mime, "1.3.1", "30ce04ab3175b6ad0bdce0035cba77bba68b813d523d1aac73d9781b4d193cf8", [:mix], [], "hexpm"},
13 | "phoenix": {:hex, :phoenix, "1.4.9", "746d098e10741c334d88143d3c94cab1756435f94387a63441792e66ec0ee974", [:mix], [{:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:phoenix_pubsub, "~> 1.1", [hex: :phoenix_pubsub, repo: "hexpm", optional: false]}, {:plug, "~> 1.8.1 or ~> 1.9", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 1.0 or ~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm"},
14 | "phoenix_ecto": {:hex, :phoenix_ecto, "4.0.0", "c43117a136e7399ea04ecaac73f8f23ee0ffe3e07acfcb8062fe5f4c9f0f6531", [:mix], [{:ecto, "~> 3.0", [hex: :ecto, repo: "hexpm", optional: false]}, {:phoenix_html, "~> 2.9", [hex: :phoenix_html, repo: "hexpm", optional: true]}, {:plug, "~> 1.0", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
15 | "phoenix_html": {:hex, :phoenix_html, "2.13.3", "850e292ff6e204257f5f9c4c54a8cb1f6fbc16ed53d360c2b780a3d0ba333867", [:mix], [{:plug, "~> 1.5", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
16 | "phoenix_live_reload": {:hex, :phoenix_live_reload, "1.2.1", "274a4b07c4adbdd7785d45a8b0bb57634d0b4f45b18d2c508b26c0344bd59b8f", [:mix], [{:file_system, "~> 0.2.1 or ~> 0.3", [hex: :file_system, repo: "hexpm", optional: false]}, {:phoenix, "~> 1.4", [hex: :phoenix, repo: "hexpm", optional: false]}], "hexpm"},
17 | "phoenix_pubsub": {:hex, :phoenix_pubsub, "1.1.2", "496c303bdf1b2e98a9d26e89af5bba3ab487ba3a3735f74bf1f4064d2a845a3e", [:mix], [], "hexpm"},
18 | "plug": {:hex, :plug, "1.8.3", "12d5f9796dc72e8ac9614e94bda5e51c4c028d0d428e9297650d09e15a684478", [:mix], [{:mime, "~> 1.0", [hex: :mime, repo: "hexpm", optional: false]}, {:plug_crypto, "~> 1.0", [hex: :plug_crypto, repo: "hexpm", optional: false]}, {:telemetry, "~> 0.4", [hex: :telemetry, repo: "hexpm", optional: true]}], "hexpm"},
19 | "plug_cowboy": {:hex, :plug_cowboy, "2.1.0", "b75768153c3a8a9e8039d4b25bb9b14efbc58e9c4a6e6a270abff1cd30cbe320", [:mix], [{:cowboy, "~> 2.5", [hex: :cowboy, repo: "hexpm", optional: false]}, {:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}], "hexpm"},
20 | "plug_crypto": {:hex, :plug_crypto, "1.0.0", "18e49317d3fa343f24620ed22795ec29d4a5e602d52d1513ccea0b07d8ea7d4d", [:mix], [], "hexpm"},
21 | "postgrex": {:hex, :postgrex, "0.15.0", "dd5349161019caeea93efa42f9b22f9d79995c3a86bdffb796427b4c9863b0f0", [:mix], [{:connection, "~> 1.0", [hex: :connection, repo: "hexpm", optional: false]}, {:db_connection, "~> 2.1", [hex: :db_connection, repo: "hexpm", optional: false]}, {:decimal, "~> 1.5", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}], "hexpm"},
22 | "ranch": {:hex, :ranch, "1.7.1", "6b1fab51b49196860b733a49c07604465a47bdb78aa10c1c16a3d199f7f8c881", [:rebar3], [], "hexpm"},
23 | "telemetry": {:hex, :telemetry, "0.4.0", "8339bee3fa8b91cb84d14c2935f8ecf399ccd87301ad6da6b71c09553834b2ab", [:rebar3], [], "hexpm"},
24 | "uuid": {:hex, :uuid, "1.1.8", "e22fc04499de0de3ed1116b770c7737779f226ceefa0badb3592e64d5cfb4eb9", [:mix], [], "hexpm"},
25 | }
26 |
--------------------------------------------------------------------------------
/assets/js/lib/filetransfersender.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import EventEmitter from "events";
4 | import { PeerCommunicationProtocol } from "./peercommunication";
5 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
6 |
7 | let FILE_TRANSFER_UPLOAD_PROGRESS = "file_transfer_upload_progress";
8 | let FILE_TRANSFER_UPLOAD_COMPLETE = "file_transfer_upload_complete";
9 |
10 | /**
11 | * The file sender, which slices up the file and sends it to the peer
12 | */
13 | export class FileTransferSender extends EventEmitter {
14 | constructor(peerComm, peer_id, file, id, correlationId, transferRate) {
15 | super();
16 | this.chunkSize = 16 * 1024;
17 |
18 | this.peerComm = peerComm;
19 | this.peer_id = peer_id;
20 | this.file = file;
21 | this.id = id;
22 | this.correlationId = correlationId;
23 | this.transferRate = transferRate;
24 |
25 | this.numberOfChunks = Math.ceil(this.file.size / this.chunkSize);
26 | this.chunkNum = 0;
27 |
28 | /**
29 | * For controlling rate of transfer, the file upload is divided into segments which is
30 | * a group of chunks e.g 10 chunks can be one segment. At sending the segment the sender
31 | * will stop to send any more segments and after receiver acknowledges receiving the segment
32 | * the sender will resume sending again.
33 | * This approach can later be used to add the feature to resume and pause download as well
34 | */
35 |
36 | // The segment number tells which segment is being uploaded
37 | this.segmentNumberSending = 1;
38 | this.totalSegments = Math.ceil(this.numberOfChunks / transferRate);
39 |
40 | this.canWaitForAck = false;
41 | this.peerComm.addEventListener(
42 | PeerCommunicationConstants.PEER_DATA,
43 | this.onPeerDataReceived.bind(this)
44 | );
45 | }
46 |
47 | /**
48 | * Check if the peer data received type is ack then resume download
49 | */
50 | onPeerDataReceived({ peer_id: peer_id, data: data }) {
51 | if (
52 | data.type === "file_segment_ack" &&
53 | peer_id === this.peer_id &&
54 | data.data.transfer_id &&
55 | data.data.transfer_id == this.id &&
56 | data.data.segment_number &&
57 | data.data.segment_number === this.segmentNumberSending
58 | ) {
59 | console.log(`Segment ACK: ${data.data.segment_number}`);
60 | this.segmentNumberSending += 1;
61 | this.canWaitForAck = false;
62 | // Start asyncronously
63 | setTimeout(this.transfer.bind(this), 0);
64 | }
65 | }
66 |
67 | transfer() {
68 | (() => this.transferChunk())();
69 | }
70 |
71 | /**
72 | * Transfer the chunk to the peer
73 | */
74 | transferChunk() {
75 | if (this.chunkNum >= this.numberOfChunks) {
76 | this.emit(FILE_TRANSFER_UPLOAD_COMPLETE, {
77 | file: this.file,
78 | correlationId: this.correlationId,
79 | peerId: this.peer_id
80 | });
81 | return;
82 | }
83 |
84 | if (this.canWaitForAck && this.chunkNum % this.transferRate === 0) {
85 | // Next segment has been reached. Stop file transfer & wait for ack from receiver
86 | return;
87 | }
88 |
89 | this.canWaitForAck = true;
90 | let startByte = this.chunkSize * this.chunkNum;
91 | let chunk = this.file.slice(startByte, startByte + this.chunkSize);
92 |
93 | let reader = new FileReader();
94 |
95 | reader.onload = this.sendReadChunkAndContinue.bind(this);
96 |
97 | reader.readAsArrayBuffer(chunk);
98 | }
99 |
100 | /**
101 | * Send the chunk to the peer and once it is sent then move over to the next chunk
102 | */
103 | sendReadChunkAndContinue(event) {
104 | let buffer = event.target.result;
105 |
106 | console.log(
107 | `Sending ${this.chunkNum}/${this.numberOfChunks} length: ${buffer.length}`
108 | );
109 |
110 | this.peerComm.send(this.peer_id, {
111 | type: "file_chunk",
112 | data: {
113 | transfer_id: this.id,
114 | chunkNumber: this.chunkNum,
115 | totalChunks: this.numberOfChunks,
116 | data: this.ab2str(buffer)
117 | }
118 | });
119 |
120 | this.emit(FILE_TRANSFER_UPLOAD_PROGRESS, {
121 | chunk: this.chunkNum + 1,
122 | total: this.numberOfChunks,
123 | correlationId: this.correlationId,
124 | peerId: this.peer_id
125 | });
126 |
127 | // Incrementing chunk number
128 | this.chunkNum += 1;
129 |
130 | // Send next chunk
131 | this.transfer();
132 | }
133 |
134 | /**
135 | * Adds a listener for the file transfer upload progress
136 | */
137 | addOnProgressListener(callback) {
138 | this.on(FILE_TRANSFER_UPLOAD_PROGRESS, callback);
139 | }
140 |
141 | /**
142 | * Removes listener for the file transfer upload progress
143 | */
144 | removeOnProgressListener(callback) {
145 | this.removeListener(FILE_TRANSFER_UPLOAD_PROGRESS, callback);
146 | }
147 |
148 | /**
149 | * Adds a listener for the file transfer upload complete
150 | */
151 | addOnCompleteListener(callback) {
152 | this.on(FILE_TRANSFER_UPLOAD_COMPLETE, callback);
153 | }
154 |
155 | /**
156 | * Removes listener for the file transfer upload complete
157 | */
158 | removeOnCompleteListener(callback) {
159 | this.removeListener(FILE_TRANSFER_UPLOAD_COMPLETE, callback);
160 | }
161 |
162 | /**
163 | * Encodes the array buffer to string to send it over the wire
164 | */
165 | ab2str(buf) {
166 | return String.fromCharCode.apply(null, new Uint8Array(buf));
167 | }
168 | }
169 |
--------------------------------------------------------------------------------
/assets/js/lib/filetransferreceiver.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import EventEmitter from "events";
4 | import { PeerCommunicationProtocol } from "./peercommunication";
5 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
6 | import FileTransferConstants from "../constants/filetransferconstants";
7 |
8 | let FILE_TRANSFER_PROGRESS = "file_transfer_progress";
9 |
10 | /**
11 | * A file transfer receiver responsible for receiving data of a particular file
12 | * from the peer and combine it into a buffer. Once the download is complete
13 | * it emits the event of file download complete.
14 | */
15 | export class FileTransferReceiver extends EventEmitter {
16 | constructor(peerComm, peer_id, fileInfo, receiverId) {
17 | super();
18 | this.fileTransferCompleteEvent = "file_transfer_complete";
19 |
20 | this.peerComm = peerComm;
21 | this.receiverId = receiverId;
22 | this.peer_id = peer_id;
23 | this.fileInfo = fileInfo;
24 | this.id = this.generateId();
25 | this.receivedBuffer = null;
26 | this.chunksReceived = 0;
27 |
28 | this.transferRate = FileTransferConstants.TRANSFER_RATE;
29 | this.segmentNumberReceived = 0;
30 | }
31 |
32 | download() {
33 | // Change the file name as identifier to some id
34 | this.peerComm.send(this.peer_id, {
35 | type: "download",
36 | data: {
37 | name: this.fileInfo.name,
38 | transfer_id: this.id,
39 | transfer_rate: this.transferRate
40 | }
41 | });
42 |
43 | this.peerComm.addEventListener(
44 | PeerCommunicationConstants.PEER_DATA,
45 | this.onPeerDataReceived.bind(this)
46 | );
47 | }
48 |
49 | onPeerDataReceived({ peer_id: peer_id, data: data }) {
50 | if (
51 | data.type === "file_chunk" &&
52 | peer_id === this.peer_id &&
53 | data.data.transfer_id &&
54 | data.data.transfer_id == this.id
55 | ) {
56 | this.processFileChunk(data.data);
57 | }
58 | }
59 |
60 | /**
61 | * Process file chunks and once the donwload is complete emits the necessary event
62 | */
63 | processFileChunk({
64 | transfer_id: transfer_id,
65 | chunkNumber: chunkNum,
66 | totalChunks: totalChunks,
67 | data: data
68 | }) {
69 | console.log(
70 | `Processing file chunk ${transfer_id}, ${chunkNum}/${totalChunks}, length: ${
71 | data.length
72 | }`
73 | );
74 | this.addToBuffer(chunkNum, totalChunks, this.str2ab(data));
75 |
76 | this.chunksReceived += 1;
77 |
78 | this.sendSegmentAckIfApplicable();
79 |
80 | console.log(`Chunk Received ${this.chunksReceived}`);
81 | this.emit(FILE_TRANSFER_PROGRESS, {
82 | chunk: chunkNum + 1,
83 | total: totalChunks,
84 | correlationId: this.receiverId
85 | });
86 |
87 | if (chunkNum === totalChunks - 1 && this.allChunksDownloaded()) {
88 | let receivedFile = new window.Blob(this.receivedBuffer);
89 | this.emit(this.fileTransferCompleteEvent, {
90 | file: this.fileInfo,
91 | blob: receivedFile,
92 | correlationId: this.receiverId
93 | });
94 |
95 | // Remove any resources as the download is complete
96 | this.destructResources();
97 | }
98 | }
99 |
100 | /**
101 | * Send the acknowledgment for the segment being received
102 | */
103 | sendSegmentAckIfApplicable() {
104 | // We check for chunks Received +1 as at the sender side it is one greate
105 | // TODO: Improve this logic. It is ugly
106 | if ((this.chunksReceived + 1) % this.transferRate === 0) {
107 | this.segmentNumberReceived += 1;
108 |
109 | this.peerComm.send(this.peer_id, {
110 | type: "file_segment_ack",
111 | data: {
112 | transfer_id: this.id,
113 | segment_number: this.segmentNumberReceived
114 | }
115 | });
116 |
117 | console.log(`Segment ACK sent: ${this.segmentNumberReceived}`);
118 | }
119 | }
120 |
121 | /**
122 | * Removes the resources held by this. Should be called after the download is complete
123 | */
124 | destructResources() {
125 | this.peerComm.removeEventListener(
126 | PeerCommunicationConstants.PEER_DATA,
127 | this.onPeerDataReceived.bind(this)
128 | );
129 | this.receivedBuffer = null;
130 | }
131 |
132 | /**
133 | * Checks if all the chunks has been downloaded by seeing if any of them is
134 | * undefined or not
135 | */
136 | allChunksDownloaded() {
137 | for (let i = this.receivedBuffer - 1; i >= 0; i--) {
138 | if (!this.receivedBuffer[i]) {
139 | return false;
140 | }
141 | }
142 |
143 | return true;
144 | }
145 |
146 | addToBuffer(chunkNumber, totalChunks, data) {
147 | if (this.receivedBuffer === null) {
148 | this.receivedBuffer = new Array(totalChunks);
149 | }
150 |
151 | this.receivedBuffer[chunkNumber] = data;
152 | }
153 |
154 | /**
155 | * Adds a listener for the file transfer progress
156 | */
157 | addOnProgressListener(callback) {
158 | this.on(FILE_TRANSFER_PROGRESS, callback);
159 | }
160 |
161 | /**
162 | * Removes listener for the file transfer progress
163 | */
164 | removeOnProgressListener(callback) {
165 | this.removeListener(FILE_TRANSFER_PROGRESS, callback);
166 | }
167 |
168 | /**
169 | * Adds a listener for the file transfer complete
170 | */
171 | addOnCompleteListener(callback) {
172 | this.on(this.fileTransferCompleteEvent, callback);
173 | }
174 |
175 | /**
176 | * Removes listener for the file transfer complete
177 | */
178 | removeOnCompleteListener(callback) {
179 | this.removeListener(this.fileTransferCompleteEvent, callback);
180 | }
181 |
182 | /**
183 | * Converts the encoded string received from peer to array buffer
184 | */
185 | str2ab(str) {
186 | var buf = new ArrayBuffer(str.length); // 1 bytes for each char
187 | var bufView = new Uint8Array(buf);
188 | for (var i = 0, strLen = str.length; i < strLen; i++) {
189 | bufView[i] = str.charCodeAt(i);
190 | }
191 | return buf;
192 | }
193 |
194 | /**
195 | * Generates a random identifier
196 | */
197 | generateId() {
198 | let text = "";
199 | let possible =
200 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
201 |
202 | for (let i = 0; i < 5; i++)
203 | text += possible.charAt(Math.floor(Math.random() * possible.length));
204 |
205 | return text;
206 | }
207 | }
208 |
--------------------------------------------------------------------------------
/assets/js/lib/filetransfermanager.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 | import EventEmitter from "events";
3 | import { PeerCommunicationProtocol } from "./peercommunication";
4 | import { FileTransferSender } from "./filetransfersender";
5 | import { FileTransferReceiver } from "./filetransferreceiver";
6 | import { AppDispatcher } from "../appdispatcher";
7 | import { FileInfoStore } from "../stores/fileinfostore";
8 | import FileTransferConstants from "../constants/filetransferconstants";
9 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
10 |
11 | let _instance = null;
12 |
13 | /**
14 | * The file manager for downloading and send files requested by the peer
15 | */
16 | export class FileTransferManager extends EventEmitter {
17 | static instance() {
18 | return _instance;
19 | }
20 |
21 | static initialize(peer_id) {
22 | if (_instance === null) {
23 | _instance = new FileTransferManager(
24 | PeerCommunicationProtocol.instance(),
25 | peer_id
26 | );
27 | }
28 | }
29 |
30 | constructor(peerComm, peer_id) {
31 | super();
32 |
33 | // Registering the store with dispatcher
34 | this.dispatcherIndex = AppDispatcher.register(
35 | this.dispatchAction.bind(this)
36 | );
37 |
38 | this.peerComm = peerComm;
39 | this.files = [];
40 | this.peer_id = peer_id;
41 | this.peerComm.addEventListener(
42 | PeerCommunicationConstants.PEER_DATA,
43 | this.onPeerDataReceived.bind(this)
44 | );
45 | FileInfoStore.instance().addChangeListener(this.onFilesUpdated.bind(this));
46 | }
47 |
48 | dispatchAction({ source: source, action: action }) {
49 | switch (action.actionType) {
50 | case FileTransferConstants.TRANSFER_FILE_DOWNLOAD:
51 | this.downloadFile(
52 | this.peer_id,
53 | action.file,
54 | this.onFileDownloaded.bind(this),
55 | this.onFileDownloadProgress.bind(this),
56 | action.correlationId
57 | );
58 | }
59 | }
60 |
61 | /*******************************************
62 | *** Download complete & progress events ***
63 | *******************************************/
64 |
65 | /**
66 | * Adds a listener for the file transfer complete
67 | */
68 | addOnFileDownloadCompleteListener(callback) {
69 | this.on(FileTransferConstants.TRANSFER_FILE_DOWNLOAD_COMPLETE, callback);
70 | }
71 |
72 | /**
73 | * Removes listener for the file transfer complete
74 | */
75 | removeOnFileDownloadCompleteListener(callback) {
76 | this.removeListener(
77 | FileTransferConstants.TRANSFER_FILE_DOWNLOAD_COMPLETE,
78 | callback
79 | );
80 | }
81 |
82 | /**
83 | * Adds a listener for the file transfer progress
84 | */
85 | addOnFileDownloadProgressListener(callback) {
86 | this.on(FileTransferConstants.TRANSFER_FILE_DOWNLOAD_PROGRESS, callback);
87 | }
88 |
89 | /**
90 | * Removes listener for the file transfer progress
91 | */
92 | removeOnFileDownloadProgressListener(callback) {
93 | this.removeListener(
94 | FileTransferConstants.TRANSFER_FILE_DOWNLOAD_PROGRESS,
95 | callback
96 | );
97 | }
98 |
99 | /**
100 | * Called when the file chunk is downloaded
101 | */
102 | onFileDownloadProgress(f) {
103 | console.log("File download progress", f);
104 | this.emit(FileTransferConstants.TRANSFER_FILE_DOWNLOAD_PROGRESS, f);
105 | }
106 |
107 | /**
108 | * Called when the file is downloaded from the peer
109 | */
110 | onFileDownloaded(f) {
111 | console.log("File Downloaded", f);
112 | this.emit(FileTransferConstants.TRANSFER_FILE_DOWNLOAD_COMPLETE, f);
113 | }
114 |
115 | /*****************************************
116 | *** Upload complete & progress events ***
117 | *****************************************/
118 |
119 | /**
120 | * Adds a listener for the file upload complete
121 | */
122 | addOnFileUploadCompleteListener(callback) {
123 | this.on(FileTransferConstants.TRANSFER_FILE_UPLOAD_COMPLETE, callback);
124 | }
125 |
126 | /**
127 | * Removes listener for the file upload complete
128 | */
129 | removeOnFileUploadCompleteListener(callback) {
130 | this.removeListener(
131 | FileTransferConstants.TRANSFER_FILE_UPLOAD_COMPLETE,
132 | callback
133 | );
134 | }
135 |
136 | /**
137 | * Adds a listener for the file upload progress
138 | */
139 | addOnFileUploadProgressListener(callback) {
140 | this.on(FileTransferConstants.TRANSFER_FILE_UPLOAD_PROGRESS, callback);
141 | }
142 |
143 | /**
144 | * Removes listener for the file upload progress
145 | */
146 | removeOnFileUploadProgressListener(callback) {
147 | this.removeListener(
148 | FileTransferConstants.TRANSFER_FILE_UPLOAD_PROGRESS,
149 | callback
150 | );
151 | }
152 |
153 | /**
154 | * Called when the file chunk is uploaded
155 | */
156 | onFileUploadProgress(f) {
157 | console.log("File upload progress", f);
158 | this.emit(FileTransferConstants.TRANSFER_FILE_UPLOAD_PROGRESS, f);
159 | }
160 |
161 | /**
162 | * Called whent the file upload to a particular peer is completed
163 | */
164 | onFileUploaded(f) {
165 | console.log("File Uploaded", f);
166 | this.emit(FileTransferConstants.TRANSFER_FILE_UPLOAD_COMPLETE, f);
167 | }
168 |
169 | /**
170 | * Called when the data from peer is received. If the request type is "download" it
171 | * initiates the upload of file
172 | */
173 | onPeerDataReceived({ peer_id: peer_id, data: data }) {
174 | let { type: type, data: payload } = data;
175 | if (type === "download") {
176 | this.startFileTransfer(peer_id, payload);
177 | }
178 | }
179 |
180 | onFilesUpdated(files) {
181 | this.files = files;
182 | }
183 |
184 | /**
185 | * Start upload of the file
186 | */
187 | startFileTransfer(
188 | peer_id,
189 | { name: name, transfer_id: id, transfer_rate: transferRate }
190 | ) {
191 | let file = this.files.filter(f => f.name === name);
192 | if (file && file.length > 0) {
193 | // Take the first file
194 | file = file[0];
195 |
196 | let correlationId = this.files.indexOf(file);
197 |
198 | let uploader = new FileTransferSender(
199 | this.peerComm,
200 | peer_id,
201 | file,
202 | id,
203 | correlationId,
204 | transferRate
205 | );
206 | uploader.addOnCompleteListener(this.onFileUploaded.bind(this));
207 | uploader.addOnProgressListener(this.onFileUploadProgress.bind(this));
208 |
209 | // Start the file upload
210 | uploader.transfer();
211 | }
212 | }
213 |
214 | /**
215 | * Download the file from the peer
216 | */
217 | downloadFile(peer_id, file, callback, progressCallback, correlationId) {
218 | let downloader = new FileTransferReceiver(
219 | this.peerComm,
220 | peer_id,
221 | file,
222 | correlationId
223 | );
224 | downloader.addOnCompleteListener(callback);
225 | downloader.addOnProgressListener(progressCallback);
226 |
227 | // Start the file download
228 | downloader.download();
229 | }
230 | }
231 |
--------------------------------------------------------------------------------
/assets/js/stores/fileinfostore.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import EventEmitter from "events";
4 | import { PeerCommunicationProtocol } from "../lib/peercommunication";
5 | import FileConstants from "../constants/fileconstants";
6 | import { AppDispatcher } from "../appdispatcher";
7 | import { FileTransferManager } from "../lib/filetransfermanager";
8 | import { FileTransferActions } from "../actions/filetransferaction";
9 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
10 |
11 | const UPDATE_FILE_EVENT = "file-update-event";
12 |
13 | // Using it to make it a singleton class
14 | let _instance = null;
15 |
16 | /**
17 | * The flux store which handles all the file information including the
18 | * blob associated with it once it downloads
19 | */
20 | export class FileInfoStore extends EventEmitter {
21 | static instance() {
22 | return _instance;
23 | }
24 |
25 | static initialize(isInitiator) {
26 | if (this.instance() === null) {
27 | _instance = new FileInfoStore(
28 | PeerCommunicationProtocol.instance(),
29 | isInitiator
30 | );
31 | }
32 | }
33 |
34 | constructor(peerComm, isInitiator) {
35 | super();
36 |
37 | this.peerComm = peerComm;
38 | this.isInitiator = isInitiator;
39 | this.files = [];
40 |
41 | // Registering the store with dispatcher
42 | this.dispatcherIndex = AppDispatcher.register(
43 | this.dispatchAction.bind(this)
44 | );
45 |
46 | // The initiator only sends data and is not bothered about receiving and reciprocal for the client peers
47 | if (this.isInitiator) {
48 | this.peerComm.addEventListener(
49 | PeerCommunicationConstants.PEER_CONNECTED,
50 | this.onPeerConnected.bind(this)
51 | );
52 | } else {
53 | this.peerComm.addEventListener(
54 | PeerCommunicationConstants.PEER_DATA,
55 | this.onPeerDataReceived.bind(this)
56 | );
57 | }
58 | }
59 |
60 | initialize() {
61 | // Listeners for download progress and complete
62 | FileTransferManager.instance().addOnFileDownloadCompleteListener(
63 | this.onFileDownloadComplete.bind(this)
64 | );
65 | FileTransferManager.instance().addOnFileDownloadProgressListener(
66 | this.onFileDownloadProgress.bind(this)
67 | );
68 |
69 | // Listeners for upload progress and complete
70 | FileTransferManager.instance().addOnFileUploadCompleteListener(
71 | this.onFileUploadComplete.bind(this)
72 | );
73 | FileTransferManager.instance().addOnFileUploadProgressListener(
74 | this.onFileUploadProgress.bind(this)
75 | );
76 | }
77 |
78 | dispatchAction({ source: source, action: action }) {
79 | switch (action.actionType) {
80 | case FileConstants.FILE_CREATE:
81 | this.addFile(action.file);
82 | this.notifyUpdatedFiles(this.files);
83 | break;
84 |
85 | case FileConstants.FILE_DESTROY:
86 | this.removeFile(action.id);
87 | this.notifyUpdatedFiles(this.files);
88 | break;
89 |
90 | case FileConstants.FILE_DOWNLOAD:
91 | let index = action.id;
92 | this.callAsync(this.downloadFile.bind(this, index));
93 | break;
94 | }
95 |
96 | return true;
97 | }
98 |
99 | callAsync(func) {
100 | setTimeout(func, 0);
101 | }
102 |
103 | getAll() {
104 | return this.files;
105 | }
106 |
107 | onFileDownloadComplete({ file: file, blob: fileBlob, correlationId: index }) {
108 | this.files[index].downloadedBlob = fileBlob;
109 | this.files[index].downloadUrl = window.URL.createObjectURL(fileBlob);
110 | this.notifyUpdatedFiles(this.files);
111 | }
112 |
113 | onFileDownloadProgress({ chunk: chunk, total: total, correlationId: index }) {
114 | this.files[index].downloadProgress = Math.ceil((chunk / total) * 100);
115 | this.notifyUpdatedFiles(this.files);
116 | }
117 |
118 | onFileUploadComplete({ file: file, correlationId: index, peerId: peer_id }) {
119 | if (
120 | this.files[index].uploadProgress &&
121 | this.files[index].uploadProgress[peer_id]
122 | ) {
123 | delete this.files[index].uploadProgress[peer_id];
124 |
125 | this.notifyUpdatedFiles(this.files);
126 | }
127 | }
128 |
129 | onFileUploadProgress({
130 | chunk: chunk,
131 | total: total,
132 | correlationId: index,
133 | peerId: peer_id
134 | }) {
135 | if (!this.files[index].uploadProgress) {
136 | this.files[index].uploadProgress = {};
137 | }
138 |
139 | this.files[index].uploadProgress[peer_id] = {
140 | chunk: chunk,
141 | total: total,
142 | percentage: Math.ceil((chunk / total) * 100)
143 | };
144 |
145 | this.notifyUpdatedFiles(this.files);
146 | }
147 |
148 | onPeerConnected({ peer_id: peer_id, peer_comm: p }) {
149 | p.send(peer_id, this.getDataForPeer(this.files));
150 | }
151 |
152 | /**
153 | * Add a file to the list and send that information to all the connected peers
154 | */
155 | addFile(file) {
156 | console.log("File added, sending to all peers", file);
157 | this.files.push(file);
158 | this.peerComm.sendToAllConnectedPeers(this.getDataForPeer(this.files));
159 | this.notifyUpdatedFiles(this.files);
160 | }
161 |
162 | /**
163 | * Removes a file to the list and send that information to all connected peers
164 | */
165 | removeFile(index) {
166 | console.log("File removed, sending to all peers", index);
167 | this.files.splice(index, 1);
168 | this.peerComm.sendToAllConnectedPeers(this.getDataForPeer(this.files));
169 | this.notifyUpdatedFiles(this.files);
170 | }
171 |
172 | /**
173 | * Download the file
174 | */
175 | downloadFile(index) {
176 | let file = this.files[index];
177 | FileTransferActions.download(file, index);
178 | }
179 |
180 | getDataForPeer(files) {
181 | return {
182 | type: "files",
183 | data: files.map(f => {
184 | return { name: f.name, size: f.size };
185 | })
186 | };
187 | }
188 |
189 | /**
190 | * Called when the data is received from the peer. In case of "files" type it updates
191 | * the file information in the store
192 | */
193 | onPeerDataReceived({ peer_id: peer_id, data: data }) {
194 | console.log(`FileInfoStore: Peer Data Received from ${peer_id}`);
195 | if (data.type === "files") {
196 | this.onPeerFilesReceived(data.data);
197 | }
198 | }
199 |
200 | onPeerFilesReceived(files) {
201 | this.files = this.mergeFileInformation(this.files, files);
202 |
203 | this.notifyUpdatedFiles(this.files);
204 | }
205 |
206 | /**
207 | * Merges file information with existing files, so that if a file is dowloaded before
208 | * then that information is still retained
209 | */
210 | mergeFileInformation(filesToMergeTo, filesToMerge) {
211 | let files = [];
212 |
213 | for (let file of filesToMerge) {
214 | let found = filesToMergeTo.filter(f => {
215 | return f.name === file.name && f.size === file.size && f.downloadUrl;
216 | });
217 |
218 | if (found && found.length > 0) {
219 | files.push(found[0]);
220 | } else {
221 | files.push(file);
222 | }
223 | }
224 |
225 | this.clearOldArray(filesToMergeTo);
226 |
227 | return files;
228 | }
229 |
230 | clearOldArray(oldFiles) {
231 | for (let i = 0; i < oldFiles.length; i++) {
232 | oldFiles[i] = null;
233 | }
234 | }
235 |
236 | notifyUpdatedFiles(files) {
237 | this.emit(UPDATE_FILE_EVENT, files);
238 | }
239 |
240 | addChangeListener(callback) {
241 | this.on(UPDATE_FILE_EVENT, callback);
242 | }
243 |
244 | removeChangeListener(callback) {
245 | this.removeListener(UPDATE_FILE_EVENT, callback);
246 | }
247 | }
248 |
--------------------------------------------------------------------------------
/assets/js/lib/peercommunication.js:
--------------------------------------------------------------------------------
1 | /*jshint esnext: true*/
2 |
3 | import EventEmitter from "events";
4 | import { Socket } from "phoenix";
5 | import PeerCommunicationConstants from "../constants/PeerCommunicationConstants";
6 | import SimplePeer from "simple-peer";
7 |
8 | let _instance = null;
9 |
10 | /**
11 | * A communication handler between all the peers and between the broker and this peer to
12 | * get registered
13 | */
14 | export class PeerCommunicationProtocol extends EventEmitter {
15 | static instance() {
16 | return _instance;
17 | }
18 |
19 | static initialize(initiator, onDataReceived, onConnected, onRTCConnected) {
20 | if (_instance === null) {
21 | _instance = new PeerCommunicationProtocol(
22 | initiator,
23 | onDataReceived,
24 | onConnected,
25 | onRTCConnected
26 | );
27 | }
28 | }
29 |
30 | constructor(initiator, onDataReceived, onConnected, onRTCConnected) {
31 | super();
32 |
33 | this.id = null;
34 | this.initiator = initiator;
35 | this.peers = {};
36 | this.onDataReceivedExternal = onDataReceived;
37 | this.onConnectedExternal = onConnected;
38 | this.onRTCConnectedExternal = onRTCConnected;
39 |
40 | let socket = new Socket("/ws", {
41 | logger: (kind, msg, data) => {
42 | console.log(`${kind}: ${msg}`, data);
43 | }
44 | });
45 |
46 | socket.connect();
47 |
48 | socket.onClose(e => console.log("CLOSE", e));
49 |
50 | this.chan = socket.channel("broker:match", {});
51 |
52 | this.chan.join().receive("ok", this.onWSJoined.bind(this));
53 | // .after(1000, () => console.log("Connection interuption"));
54 |
55 | this.chan.onError(this.onWSError.bind(this));
56 | this.chan.onClose(this.onWSClose.bind(this));
57 | }
58 |
59 | addEventListener(event, callback) {
60 | this.on(event, callback);
61 | return this;
62 | }
63 |
64 | removeEventListener(event, callback) {
65 | this.removeListener(event, callback);
66 | return this;
67 | }
68 |
69 | createRTCPeer(initiator, peer_id) {
70 | return new RTCCommunication(
71 | initiator,
72 | o => {
73 | this.onRTCOfferCreated(peer_id, o);
74 | },
75 | a => {
76 | this.onRTCAnswerCreated(peer_id, a);
77 | },
78 | d => {
79 | this.onRTCDataReceived(peer_id, d);
80 | },
81 | c => {
82 | this.onRTCConnected(peer_id, c);
83 | }
84 | );
85 | }
86 |
87 | onWSJoined() {
88 | console.log("WS: Joined");
89 |
90 | this.chan.on("registered", this.onWSRegistered.bind(this));
91 | this.chan.push("register", {});
92 | }
93 |
94 | /* Web socket communication */
95 | onWSError(event) {
96 | console.log("WS: Error", event);
97 | }
98 |
99 | onWSClose(event) {
100 | console.log("WS: Close", event);
101 | }
102 |
103 | /**
104 | * After register with the broker is successful, handle messages from the broker as follows
105 | * error_connect: Error in connecting to peer
106 | * peer_connect: Request to connect to a peer
107 | * offer: Offer received from the connecting peer, should reply with answer
108 | * answer: Answer received from the offer accepting peer.
109 | */
110 | onWSRegistered(msg) {
111 | console.log("WS: Registered", msg);
112 | this.id = msg.id;
113 | this.chan.leave();
114 |
115 | let socket = new Socket("/peer", {
116 | logger: (kind, msg, data) => {
117 | console.log(`${kind}: ${msg}`, data);
118 | }
119 | });
120 |
121 | socket.connect();
122 |
123 | socket.onClose(e => console.log("CLOSE", e));
124 |
125 | this.chan = socket.channel("peer:" + this.id, {});
126 |
127 | this.chan.join().receive("ok", this.onWSPeerJoined.bind(this));
128 | // .after(1000, () => console.log("Connection interuption"));
129 |
130 | this.chan.onError(this.onWSError.bind(this));
131 | this.chan.onClose(this.onWSClose.bind(this));
132 | }
133 |
134 | onWSPeerJoined() {
135 | this.chan.on("error_connect", this.onWSPeerErrorConnect.bind(this));
136 | this.chan.on("peer_connect", this.onWSPeerConnect.bind(this));
137 | this.chan.on("offer", this.onWSOffer.bind(this));
138 | this.chan.on("answer", this.onWSAnswer.bind(this));
139 |
140 | this.emit(PeerCommunicationConstants.CONNECTED, this);
141 |
142 | if (this.onConnectedExternal) {
143 | this.onConnectedExternal(this);
144 | }
145 | }
146 |
147 | onWSPeerErrorConnect(msg) {
148 | console.log("WS: Peer Connect Error", msg);
149 | this.emit(PeerCommunicationConstants.PEER_DOES_NOT_EXIST, msg);
150 | }
151 |
152 | onWSPeerConnect(msg) {
153 | console.log("WS: Peer Connection", msg);
154 | this.peers[msg.peer_id] = this.createRTCPeer(this.initiator, msg.peer_id);
155 | }
156 |
157 | onWSOffer(msg) {
158 | console.log("WS: Got Offer", msg);
159 | this.peers[msg.peer_id].signal(msg.offer);
160 | }
161 |
162 | onWSAnswer(msg) {
163 | console.log("WS: Got Answer", msg);
164 | this.peers[msg.peer_id].signal(msg.answer);
165 | }
166 |
167 | /* RTC communication */
168 | onRTCOfferCreated(peer_id, offer) {
169 | console.log(`RTC: Offer created ${peer_id}`, offer);
170 | this.chan.push("offer", {
171 | offer: offer,
172 | peer_id: peer_id,
173 | sender_id: this.id
174 | });
175 | }
176 |
177 | onRTCAnswerCreated(peer_id, answer) {
178 | console.log(`RTC: Answer created ${peer_id}`, answer);
179 | this.chan.push("answer", {
180 | answer: answer,
181 | peer_id: peer_id,
182 | sender_id: this.id
183 | });
184 | }
185 |
186 | onRTCDataReceived(peer_id, data) {
187 | console.log(`RTC: Data received ${peer_id}`);
188 |
189 | this.emit(PeerCommunicationConstants.PEER_DATA, {
190 | peer_id: peer_id,
191 | data: data
192 | });
193 |
194 | if (this.onDataReceivedExternal) {
195 | this.onDataReceivedExternal(peer_id, data);
196 | }
197 | }
198 |
199 | onRTCConnected(peer_id, rtcClient) {
200 | console.log(`RTC: Connected ${peer_id}`);
201 |
202 | this.emit(PeerCommunicationConstants.PEER_CONNECTED, {
203 | peer_id: peer_id,
204 | peer_comm: this
205 | });
206 |
207 | if (this.onRTCConnectedExternal) {
208 | this.onRTCConnectedExternal(rtcClient);
209 | }
210 | }
211 |
212 | /* Other functions */
213 | connect(peer_id) {
214 | this.peers[peer_id] = this.createRTCPeer(this.initiator, peer_id);
215 | this.chan.push("connect", { peer_id: peer_id, sender_id: this.id });
216 | }
217 |
218 | /**
219 | * Send `data` to connected peer with id `peer_id`
220 | */
221 | send(peer_id, data) {
222 | this.peers[peer_id].send(data);
223 | }
224 |
225 | /**
226 | * Send `data` to all the connected peers
227 | */
228 | sendToAllConnectedPeers(data) {
229 | for (let peer_id in this.peers) {
230 | if (this.peers[peer_id] && this.peers[peer_id].isConnected) {
231 | // Call it in an async way so any failure does not effect sending to other peers
232 | setTimeout(() => {
233 | let peer = this.peers[peer_id];
234 | if (peer.isPeerConnected()) {
235 | peer.send(data);
236 | } else {
237 | delete this.peers[peer_id];
238 | }
239 | }, 0);
240 | }
241 | }
242 | }
243 |
244 | getId() {
245 | return this.id;
246 | }
247 | }
248 |
249 | /**
250 | * A handler for WebRTC communication with one peer
251 | */
252 | export class RTCCommunication {
253 | constructor(
254 | initiator,
255 | onOfferCreated,
256 | onAnswerCreated,
257 | onDataReceived,
258 | onRTCConnected
259 | ) {
260 | this.peer = new SimplePeer({ initiator: initiator, trickle: false });
261 | this.initiator = initiator;
262 | this.onOfferCreated = onOfferCreated;
263 | this.onAnswerCreated = onAnswerCreated;
264 | this.onDataReceived = onDataReceived;
265 | this.onRTCConnected = onRTCConnected;
266 | this.isConnected = false;
267 |
268 | this.peer.on("error", this.onError.bind(this));
269 | this.peer.on("signal", this.onSignal.bind(this));
270 | this.peer.on("connect", this.onConnect.bind(this));
271 | this.peer.on("data", this.onData.bind(this));
272 | }
273 |
274 | send(data) {
275 | this.peer.send(JSON.stringify(data));
276 | }
277 |
278 | signal(data) {
279 | this.peer.signal(data);
280 | }
281 |
282 | onError(error) {
283 | console.log("RTC: Error", error);
284 | }
285 |
286 | onSignal(data) {
287 | console.log("RTC: Data", data);
288 | if (this.initiator) {
289 | this.offer = data;
290 | if (this.onOfferCreated) {
291 | this.onOfferCreated(this.offer);
292 | }
293 | } else {
294 | this.answer = data;
295 | if (this.onAnswerCreated) {
296 | this.onAnswerCreated(this.answer);
297 | }
298 | }
299 | }
300 |
301 | onConnect() {
302 | console.log("RTC: Connected");
303 | this.isConnected = true;
304 | if (this.onRTCConnected) {
305 | this.onRTCConnected(this);
306 | }
307 | }
308 |
309 | isPeerConnected() {
310 | return this.peer.connected;
311 | }
312 |
313 | onData(data) {
314 | console.log("RTC: Data received");
315 | if (this.onDataReceived) {
316 | this.onDataReceived(JSON.parse(data.toString("utf-8")));
317 | }
318 | }
319 | }
320 |
--------------------------------------------------------------------------------
/assets/css/phoenix.css:
--------------------------------------------------------------------------------
1 | /* Includes some default style for the starter application.
2 | * This can be safely deleted to start fresh.
3 | */
4 |
5 | /* Milligram v1.3.0 https://milligram.github.io
6 | * Copyright (c) 2017 CJ Patoilo Licensed under the MIT license
7 | */
8 |
9 | *,*:after,*:before{box-sizing:inherit}html{box-sizing:border-box;font-size:62.5%}body{color:#000000;font-family:'Helvetica', 'Arial', sans-serif;font-size:1.6em;font-weight:300;line-height:1.6}blockquote{border-left:0.3rem solid #d1d1d1;margin-left:0;margin-right:0;padding:1rem 1.5rem}blockquote *:last-child{margin-bottom:0}.button,button,input[type='button'],input[type='reset'],input[type='submit']{background-color:#0069d9;border:0.1rem solid #0069d9;border-radius:.4rem;color:#fff;cursor:pointer;display:inline-block;font-size:1.1rem;font-weight:700;height:3.8rem;letter-spacing:.1rem;line-height:3.8rem;padding:0 3.0rem;text-align:center;text-decoration:none;text-transform:uppercase;white-space:nowrap}.button:focus,.button:hover,button:focus,button:hover,input[type='button']:focus,input[type='button']:hover,input[type='reset']:focus,input[type='reset']:hover,input[type='submit']:focus,input[type='submit']:hover{background-color:#606c76;border-color:#606c76;color:#fff;outline:0}.button[disabled],button[disabled],input[type='button'][disabled],input[type='reset'][disabled],input[type='submit'][disabled]{cursor:default;opacity:.5}.button[disabled]:focus,.button[disabled]:hover,button[disabled]:focus,button[disabled]:hover,input[type='button'][disabled]:focus,input[type='button'][disabled]:hover,input[type='reset'][disabled]:focus,input[type='reset'][disabled]:hover,input[type='submit'][disabled]:focus,input[type='submit'][disabled]:hover{background-color:#0069d9;border-color:#0069d9}.button.button-outline,button.button-outline,input[type='button'].button-outline,input[type='reset'].button-outline,input[type='submit'].button-outline{background-color:transparent;color:#0069d9}.button.button-outline:focus,.button.button-outline:hover,button.button-outline:focus,button.button-outline:hover,input[type='button'].button-outline:focus,input[type='button'].button-outline:hover,input[type='reset'].button-outline:focus,input[type='reset'].button-outline:hover,input[type='submit'].button-outline:focus,input[type='submit'].button-outline:hover{background-color:transparent;border-color:#606c76;color:#606c76}.button.button-outline[disabled]:focus,.button.button-outline[disabled]:hover,button.button-outline[disabled]:focus,button.button-outline[disabled]:hover,input[type='button'].button-outline[disabled]:focus,input[type='button'].button-outline[disabled]:hover,input[type='reset'].button-outline[disabled]:focus,input[type='reset'].button-outline[disabled]:hover,input[type='submit'].button-outline[disabled]:focus,input[type='submit'].button-outline[disabled]:hover{border-color:inherit;color:#0069d9}.button.button-clear,button.button-clear,input[type='button'].button-clear,input[type='reset'].button-clear,input[type='submit'].button-clear{background-color:transparent;border-color:transparent;color:#0069d9}.button.button-clear:focus,.button.button-clear:hover,button.button-clear:focus,button.button-clear:hover,input[type='button'].button-clear:focus,input[type='button'].button-clear:hover,input[type='reset'].button-clear:focus,input[type='reset'].button-clear:hover,input[type='submit'].button-clear:focus,input[type='submit'].button-clear:hover{background-color:transparent;border-color:transparent;color:#606c76}.button.button-clear[disabled]:focus,.button.button-clear[disabled]:hover,button.button-clear[disabled]:focus,button.button-clear[disabled]:hover,input[type='button'].button-clear[disabled]:focus,input[type='button'].button-clear[disabled]:hover,input[type='reset'].button-clear[disabled]:focus,input[type='reset'].button-clear[disabled]:hover,input[type='submit'].button-clear[disabled]:focus,input[type='submit'].button-clear[disabled]:hover{color:#0069d9}code{background:#f4f5f6;border-radius:.4rem;font-size:86%;margin:0 .2rem;padding:.2rem .5rem;white-space:nowrap}pre{background:#f4f5f6;border-left:0.3rem solid #0069d9;overflow-y:hidden}pre>code{border-radius:0;display:block;padding:1rem 1.5rem;white-space:pre}hr{border:0;border-top:0.1rem solid #f4f5f6;margin:3.0rem 0}input[type='email'],input[type='number'],input[type='password'],input[type='search'],input[type='tel'],input[type='text'],input[type='url'],textarea,select{-webkit-appearance:none;-moz-appearance:none;appearance:none;background-color:transparent;border:0.1rem solid #d1d1d1;border-radius:.4rem;box-shadow:none;box-sizing:inherit;height:3.8rem;padding:.6rem 1.0rem;width:100%}input[type='email']:focus,input[type='number']:focus,input[type='password']:focus,input[type='search']:focus,input[type='tel']:focus,input[type='text']:focus,input[type='url']:focus,textarea:focus,select:focus{border-color:#0069d9;outline:0}select{background:url('data:image/svg+xml;utf8, ') center right no-repeat;padding-right:3.0rem}select:focus{background-image:url('data:image/svg+xml;utf8, ')}textarea{min-height:6.5rem}label,legend{display:block;font-size:1.6rem;font-weight:700;margin-bottom:.5rem}fieldset{border-width:0;padding:0}input[type='checkbox'],input[type='radio']{display:inline}.label-inline{display:inline-block;font-weight:normal;margin-left:.5rem}.row{display:flex;flex-direction:column;padding:0;width:100%}.row.row-no-padding{padding:0}.row.row-no-padding>.column{padding:0}.row.row-wrap{flex-wrap:wrap}.row.row-top{align-items:flex-start}.row.row-bottom{align-items:flex-end}.row.row-center{align-items:center}.row.row-stretch{align-items:stretch}.row.row-baseline{align-items:baseline}.row .column{display:block;flex:1 1 auto;margin-left:0;max-width:100%;width:100%}.row .column.column-offset-10{margin-left:10%}.row .column.column-offset-20{margin-left:20%}.row .column.column-offset-25{margin-left:25%}.row .column.column-offset-33,.row .column.column-offset-34{margin-left:33.3333%}.row .column.column-offset-50{margin-left:50%}.row .column.column-offset-66,.row .column.column-offset-67{margin-left:66.6666%}.row .column.column-offset-75{margin-left:75%}.row .column.column-offset-80{margin-left:80%}.row .column.column-offset-90{margin-left:90%}.row .column.column-10{flex:0 0 10%;max-width:10%}.row .column.column-20{flex:0 0 20%;max-width:20%}.row .column.column-25{flex:0 0 25%;max-width:25%}.row .column.column-33,.row .column.column-34{flex:0 0 33.3333%;max-width:33.3333%}.row .column.column-40{flex:0 0 40%;max-width:40%}.row .column.column-50{flex:0 0 50%;max-width:50%}.row .column.column-60{flex:0 0 60%;max-width:60%}.row .column.column-66,.row .column.column-67{flex:0 0 66.6666%;max-width:66.6666%}.row .column.column-75{flex:0 0 75%;max-width:75%}.row .column.column-80{flex:0 0 80%;max-width:80%}.row .column.column-90{flex:0 0 90%;max-width:90%}.row .column .column-top{align-self:flex-start}.row .column .column-bottom{align-self:flex-end}.row .column .column-center{-ms-grid-row-align:center;align-self:center}@media (min-width: 40rem){.row{flex-direction:row;margin-left:-1.0rem;width:calc(100% + 2.0rem)}.row .column{margin-bottom:inherit;padding:0 1.0rem}}a{color:#0069d9;text-decoration:none}a:focus,a:hover{color:#606c76}dl,ol,ul{list-style:none;margin-top:0;padding-left:0}dl dl,dl ol,dl ul,ol dl,ol ol,ol ul,ul dl,ul ol,ul ul{font-size:90%;margin:1.5rem 0 1.5rem 3.0rem}ol{list-style:decimal inside}ul{list-style:circle inside}.button,button,dd,dt,li{margin-bottom:1.0rem}fieldset,input,select,textarea{margin-bottom:1.5rem}blockquote,dl,figure,form,ol,p,pre,table,ul{margin-bottom:2.5rem}table{border-spacing:0;width:100%}td,th{border-bottom:0.1rem solid #e1e1e1;padding:1.2rem 1.5rem;text-align:left}td:first-child,th:first-child{padding-left:0}td:last-child,th:last-child{padding-right:0}b,strong{font-weight:bold}p{margin-top:0}h1,h2,h3,h4,h5,h6{font-weight:300;letter-spacing:-.1rem;margin-bottom:2.0rem;margin-top:0}h1{font-size:4.6rem;line-height:1.2}h2{font-size:3.6rem;line-height:1.25}h3{font-size:2.8rem;line-height:1.3}h4{font-size:2.2rem;letter-spacing:-.08rem;line-height:1.35}h5{font-size:1.8rem;letter-spacing:-.05rem;line-height:1.5}h6{font-size:1.6rem;letter-spacing:0;line-height:1.4}img{max-width:100%}.clearfix:after{clear:both;content:' ';display:table}.float-left{float:left}.float-right{float:right}
10 |
11 | /* General style */
12 | h1{font-size: 3.6rem; line-height: 1.25}
13 | h2{font-size: 2.8rem; line-height: 1.3}
14 | h3{font-size: 2.2rem; letter-spacing: -.08rem; line-height: 1.35}
15 | h4{font-size: 1.8rem; letter-spacing: -.05rem; line-height: 1.5}
16 | h5{font-size: 1.6rem; letter-spacing: 0; line-height: 1.4}
17 | h6{font-size: 1.4rem; letter-spacing: 0; line-height: 1.2}
18 |
19 | .container{
20 | margin: 0 auto;
21 | max-width: 80.0rem;
22 | padding: 0 2.0rem;
23 | position: relative;
24 | width: 100%
25 | }
26 | select {
27 | width: auto;
28 | }
29 |
30 | /* Alerts and form errors */
31 | .alert {
32 | padding: 15px;
33 | margin-bottom: 20px;
34 | border: 1px solid transparent;
35 | border-radius: 4px;
36 | }
37 | .alert-info {
38 | color: #31708f;
39 | background-color: #d9edf7;
40 | border-color: #bce8f1;
41 | }
42 | .alert-warning {
43 | color: #8a6d3b;
44 | background-color: #fcf8e3;
45 | border-color: #faebcc;
46 | }
47 | .alert-danger {
48 | color: #a94442;
49 | background-color: #f2dede;
50 | border-color: #ebccd1;
51 | }
52 | .alert p {
53 | margin-bottom: 0;
54 | }
55 | .alert:empty {
56 | display: none;
57 | }
58 | .help-block {
59 | color: #a94442;
60 | display: block;
61 | margin: -1rem 0 2rem;
62 | }
63 |
64 | /* Phoenix promo and logo */
65 | .phx-hero {
66 | text-align: center;
67 | border-bottom: 1px solid #e3e3e3;
68 | background: #eee;
69 | border-radius: 6px;
70 | padding: 3em;
71 | margin-bottom: 3rem;
72 | font-weight: 200;
73 | font-size: 120%;
74 | }
75 | .phx-hero p {
76 | margin: 0;
77 | }
78 | .phx-logo {
79 | min-width: 300px;
80 | margin: 1rem;
81 | display: block;
82 | }
83 | .phx-logo img {
84 | width: auto;
85 | display: block;
86 | }
87 |
88 | /* Headers */
89 | header {
90 | width: 100%;
91 | background: #fdfdfd;
92 | border-bottom: 1px solid #eaeaea;
93 | margin-bottom: 2rem;
94 | }
95 | header section {
96 | align-items: center;
97 | display: flex;
98 | flex-direction: column;
99 | justify-content: space-between;
100 | }
101 | header section :first-child {
102 | order: 2;
103 | }
104 | header section :last-child {
105 | order: 1;
106 | }
107 | header nav ul,
108 | header nav li {
109 | margin: 0;
110 | padding: 0;
111 | display: block;
112 | text-align: right;
113 | white-space: nowrap;
114 | }
115 | header nav ul {
116 | margin: 1rem;
117 | margin-top: 0;
118 | }
119 | header nav a {
120 | display: block;
121 | }
122 |
123 | @media (min-width: 40.0rem) { /* Small devices (landscape phones, 576px and up) */
124 | header section {
125 | flex-direction: row;
126 | }
127 | header nav ul {
128 | margin: 1rem;
129 | }
130 | .phx-logo {
131 | flex-basis: 527px;
132 | margin: 2rem 1rem;
133 | }
134 | }
135 |
--------------------------------------------------------------------------------