Skip to content

Commit

Permalink
Apply allow_upload's chunk_timeout to push timeouts. Closes #3348
Browse files Browse the repository at this point in the history
This is useful when you want to upload very large chunk sizes that risk timing
out slower connections.
  • Loading branch information
chrismccord committed Oct 31, 2024
1 parent f685111 commit d6a0140
Show file tree
Hide file tree
Showing 9 changed files with 38 additions and 25 deletions.
9 changes: 6 additions & 3 deletions assets/js/phoenix_live_view/entry_uploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@ import {
} from "./utils"

export default class EntryUploader {
constructor(entry, chunkSize, liveSocket){
constructor(entry, config, liveSocket){
console.log(config)

This comment has been minimized.

Copy link
@SteffenDE

SteffenDE Oct 31, 2024

Collaborator

woops, forgot that one

let {chunk_size, chunk_timeout} = config
this.liveSocket = liveSocket
this.entry = entry
this.offset = 0
this.chunkSize = chunkSize
this.chunkSize = chunk_size
this.chunkTimeout = chunk_timeout
this.chunkTimer = null
this.errored = false
this.uploadChannel = liveSocket.channel(`lvu:${entry.ref}`, {token: entry.metadata()})
Expand Down Expand Up @@ -46,7 +49,7 @@ export default class EntryUploader {

pushChunk(chunk){
if(!this.uploadChannel.isJoined()){ return }
this.uploadChannel.push("chunk", chunk)
this.uploadChannel.push("chunk", chunk, this.chunkTimeout)
.receive("ok", () => {
this.entry.progress((this.offset / this.entry.file.size) * 100)
if(!this.isDone()){
Expand Down
2 changes: 1 addition & 1 deletion assets/js/phoenix_live_view/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export let maybe = (el, callback) => el && callback(el)

export let channelUploader = function (entries, onError, resp, liveSocket){
entries.forEach(entry => {
let entryUploader = new EntryUploader(entry, resp.config.chunk_size, liveSocket)
let entryUploader = new EntryUploader(entry, resp.config, liveSocket)
entryUploader.upload()
})
}
3 changes: 2 additions & 1 deletion lib/phoenix_live_view/upload.ex
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,8 @@ defmodule Phoenix.LiveView.Upload do
client_meta = %{
max_file_size: conf.max_file_size,
max_entries: conf.max_entries,
chunk_size: conf.chunk_size
chunk_size: conf.chunk_size,
chunk_timeout: conf.chunk_timeout
}

{new_socket, new_conf, new_entries} = mark_preflighted(socket, conf, refs)
Expand Down
11 changes: 7 additions & 4 deletions priv/static/phoenix_live_view.cjs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions priv/static/phoenix_live_view.cjs.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions priv/static/phoenix_live_view.esm.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions priv/static/phoenix_live_view.esm.js.map

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions priv/static/phoenix_live_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,14 @@ var LiveView = (() => {

// js/phoenix_live_view/entry_uploader.js
var EntryUploader = class {
constructor(entry, chunkSize, liveSocket) {
constructor(entry, config, liveSocket) {
console.log(config);
let { chunk_size, chunk_timeout } = config;
this.liveSocket = liveSocket;
this.entry = entry;
this.offset = 0;
this.chunkSize = chunkSize;
this.chunkSize = chunk_size;
this.chunkTimeout = chunk_timeout;
this.chunkTimer = null;
this.errored = false;
this.uploadChannel = liveSocket.channel(`lvu:${entry.ref}`, { token: entry.metadata() });
Expand Down Expand Up @@ -192,7 +195,7 @@ var LiveView = (() => {
if (!this.uploadChannel.isJoined()) {
return;
}
this.uploadChannel.push("chunk", chunk).receive("ok", () => {
this.uploadChannel.push("chunk", chunk, this.chunkTimeout).receive("ok", () => {
this.entry.progress(this.offset / this.entry.file.size * 100);
if (!this.isDone()) {
this.chunkTimer = setTimeout(() => this.readNextChunk(), this.liveSocket.getLatencySim() || 0);
Expand Down Expand Up @@ -251,7 +254,7 @@ var LiveView = (() => {
var maybe = (el, callback) => el && callback(el);
var channelUploader = function(entries, onError, resp, liveSocket) {
entries.forEach((entry) => {
let entryUploader = new EntryUploader(entry, resp.config.chunk_size, liveSocket);
let entryUploader = new EntryUploader(entry, resp.config, liveSocket);
entryUploader.upload();
});
};
Expand Down
8 changes: 4 additions & 4 deletions priv/static/phoenix_live_view.min.js

Large diffs are not rendered by default.

0 comments on commit d6a0140

Please sign in to comment.