Skip to content

Commit 4904717

Browse files
committed
fix(smartcard): fix transmission service rework
1 parent 92a79ff commit 4904717

File tree

3 files changed

+31
-28
lines changed

3 files changed

+31
-28
lines changed

docs/customservices.md

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -724,24 +724,21 @@ Displays Traefik.
724724

725725
## Transmission
726726

727-
This service displays the global upload and download rates, as well as the number of active torrents from your Transmission daemon. The service communicates with the Transmission RPC interface which needs to be accessible from the browser. Make sure to configure appropriate CORS headers if accessing from a different domain.
727+
Displays the global upload and download rates, as well as the number of active torrents from your Transmission daemon.
728+
The service communicates with the Transmission RPC interface which needs to be accessible from the browser.
728729

729730
```yaml
730731
- name: "Transmission"
731732
logo: "assets/tools/sample.png"
732733
url: "http://192.168.1.2:9091" # Your Transmission web interface URL
733734
type: "Transmission"
734735
auth: "username:password" # Optional: HTTP Basic Auth
735-
interval: 5000 # Optional: Interval for updating data (ms)
736+
interval: 5000 # Optional: Interval for refreshing data (ms)
736737
target: "_blank" # Optional: HTML a tag target attribute
737738
```
738739

739-
**Configuration Options:**
740-
741-
- `auth`: Optional HTTP Basic Authentication in "username:password" format
742-
- `interval`: How often to refresh data in milliseconds
743-
744740
The service automatically handles Transmission's session management and CSRF protection.
741+
745742
## Truenas Scale
746743

747744
Displays TrueNAS version.

src/components/services/Transmission.vue

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ export default {
5858
count: null,
5959
error: null,
6060
sessionId: null,
61+
retry: 0,
6162
}),
6263
computed: {
6364
downRate: function () {
@@ -87,11 +88,6 @@ export default {
8788
transmissionRequest: async function (method) {
8889
const options = this.getRequestHeaders(method);
8990
90-
// Add HTTP Basic Auth if credentials are provided
91-
if (this.item.auth) {
92-
options.headers["Authorization"] = `Basic ${btoa(this.item.auth)}`;
93-
}
94-
9591
// Add session ID header if we have one
9692
if (this.sessionId) {
9793
options.headers["X-Transmission-Session-Id"] = this.sessionId;
@@ -101,35 +97,44 @@ export default {
10197
return await this.fetch("transmission/rpc", options);
10298
} catch (error) {
10399
// Handle Transmission's 409 session requirement
104-
if (error.message.includes("409")) {
105-
const sessionOptions = this.getRequestHeaders("session-get");
106-
107-
const sessionResponse = this.fetch(
108-
"transmission/rpc",
109-
sessionOptions,
110-
);
111-
if (error.message.includes("409")) {
112-
this.sessionId = sessionResponse.headers.get(
113-
"X-Transmission-Session-Id",
114-
);
115-
if (this.sessionId) {
116-
options.headers["X-Transmission-Session-Id"] = this.sessionId;
117-
return await this.fetch("transmission/rpc", options);
118-
}
100+
if (error.cause.status == 409 && this.retry <= 1) {
101+
const sessionId = await this.getSession();
102+
if (sessionId) {
103+
this.sessionId = sessionId;
104+
this.retry++;
105+
return this.transmissionRequest(method);
119106
}
120107
}
121108
console.error("Transmission RPC error:", error);
122109
throw error;
123110
}
124111
},
125112
getRequestHeaders: function (method) {
126-
return {
113+
const options = {
127114
method: "POST",
128115
headers: {
129116
"Content-Type": "application/json",
130117
},
131118
body: JSON.stringify({ method }),
132119
};
120+
121+
if (this.item.auth) {
122+
options.headers["Authorization"] = `Basic ${btoa(this.item.auth)}`;
123+
}
124+
125+
return options;
126+
},
127+
getSession: async function () {
128+
try {
129+
await this.fetch(
130+
"transmission/rpc",
131+
this.getRequestHeaders("session-get"),
132+
);
133+
} catch (error) {
134+
if (error.cause.status == 409) {
135+
return error.cause.headers.get("X-Transmission-Session-Id");
136+
}
137+
}
133138
},
134139
getStats: async function () {
135140
try {

src/mixins/service.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export default {
5555
if (!success) {
5656
throw new Error(
5757
`Fail to fetch ressource: (${response.status} error)`,
58+
{ cause: response },
5859
);
5960
}
6061

0 commit comments

Comments
 (0)