Skip to content

Commit eb3e263

Browse files
committed
feat: shared call to createPatch
1 parent 623846e commit eb3e263

File tree

11 files changed

+81
-76
lines changed

11 files changed

+81
-76
lines changed

api/src/unraid-api/unraid-file-modifier/file-modification.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { constants } from 'fs';
33
import { access, readFile, unlink, writeFile } from 'fs/promises';
44
import { basename, dirname, join } from 'path';
55

6-
import { applyPatch, parsePatch, reversePatch } from 'diff';
6+
import { applyPatch, createPatch, parsePatch, reversePatch } from 'diff';
77

88
export interface ShouldApplyWithReason {
99
shouldApply: boolean;
@@ -219,4 +219,15 @@ export abstract class FileModification {
219219
};
220220
}
221221
}
222+
223+
async createPatchWithDiff(
224+
filePath: string,
225+
originalContent: string,
226+
newContent: string
227+
): Promise<string> {
228+
const patch = createPatch(filePath, originalContent, newContent, 'original', 'modified', {
229+
context: 5,
230+
});
231+
return patch;
232+
}
222233
}

api/src/unraid-api/unraid-file-modifier/modifications/auth-request.modification.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,23 @@ export default class AuthRequestModification extends FileModification {
1515
'/usr/local/emhttp/plugins/dynamix.my.servers/unraid-components/_nuxt/' as const;
1616
id: string = 'auth-request';
1717

18+
/**
19+
* Get the list of .js files in the given directory
20+
* @param dir - The directory to search for .js files
21+
* @returns The list of .js files in the given directory
22+
*/
1823
private getJsFiles = async (dir: string) => {
1924
const { glob } = await import('glob');
2025
const files = await glob(join(dir, '**/*.js'));
21-
const baseDir = '/usr/local/emhttp'; // TODO: Make this configurable
26+
const baseDir = '/usr/local/emhttp';
2227
return files.map((file) => (file.startsWith(baseDir) ? file.slice(baseDir.length) : file));
2328
};
29+
30+
/**
31+
* Generate a patch for the auth-request.php file
32+
* @param overridePath - The path to override the default file path
33+
* @returns The patch for the auth-request.php file
34+
*/
2435
protected async generatePatch(overridePath?: string): Promise<string> {
2536
const jsFiles = await this.getJsFiles(this.webComponentsDirectory);
2637
this.logger.debug(`Found ${jsFiles.length} .js files in ${this.webComponentsDirectory}`);
@@ -42,19 +53,7 @@ export default class AuthRequestModification extends FileModification {
4253
// Create new content by finding the array declaration and adding our files after it
4354
const newContent = fileContent.replace(/(\$arrWhitelist\s*=\s*\[)/, `$1\n${filesToAddString}`);
4455

45-
// Generate and return patch
46-
const patch = createPatch(
47-
overridePath ?? this.filePath,
48-
fileContent,
49-
newContent,
50-
undefined,
51-
undefined,
52-
{
53-
context: 3,
54-
}
55-
);
56-
57-
return patch;
56+
return this.createPatchWithDiff(overridePath ?? this.filePath, fileContent, newContent);
5857
}
5958

6059
async shouldApply(): Promise<ShouldApplyWithReason> {

api/src/unraid-api/unraid-file-modifier/modifications/default-page-layout.modification.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,7 @@ export default class DefaultPageLayoutModification extends FileModification {
4646

4747
const newContent = this.applyToSource(fileContent);
4848

49-
const patch = createPatch(
50-
overridePath ?? this.filePath,
51-
fileContent,
52-
newContent,
53-
undefined,
54-
undefined,
55-
{
56-
context: 2,
57-
}
58-
);
59-
60-
return patch;
49+
return this.createPatchWithDiff(overridePath ?? this.filePath, fileContent, newContent);
6150
}
6251

6352
async shouldApply(): Promise<ShouldApplyWithReason> {

api/src/unraid-api/unraid-file-modifier/modifications/log-rotate.modification.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,11 @@ export class LogRotateModification extends FileModification {
3535
? await readFile(this.filePath, 'utf8')
3636
: '';
3737

38-
const patch = createPatch(
38+
return this.createPatchWithDiff(
3939
overridePath ?? this.filePath,
4040
currentContent,
41-
this.logRotateConfig,
42-
undefined,
43-
undefined,
44-
{
45-
context: 3,
46-
}
41+
this.logRotateConfig
4742
);
48-
49-
// After applying patch, ensure file permissions are correct
50-
await execa('chown', ['root:root', this.filePath]).catch((err) => this.logger.error(err));
51-
52-
return patch;
5343
}
5444

5545
async shouldApply(): Promise<ShouldApplyWithReason> {

api/src/unraid-api/unraid-file-modifier/modifications/notifications-page.modification.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,7 @@ export default class NotificationsPageModification extends FileModification {
1717

1818
const newContent = NotificationsPageModification.applyToSource(fileContent);
1919

20-
const patch = createPatch(
21-
overridePath ?? this.filePath,
22-
fileContent,
23-
newContent,
24-
undefined,
25-
undefined,
26-
{
27-
context: 3,
28-
}
29-
);
30-
31-
return patch;
20+
return this.createPatchWithDiff(overridePath ?? this.filePath, fileContent, newContent);
3221
}
3322

3423
async shouldApply(): Promise<ShouldApplyWithReason> {
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
Index: /usr/local/emhttp/auth-request.php
22
===================================================================
3-
--- /usr/local/emhttp/auth-request.php
4-
+++ /usr/local/emhttp/auth-request.php
5-
@@ -15,6 +15,7 @@
3+
--- /usr/local/emhttp/auth-request.php original
4+
+++ /usr/local/emhttp/auth-request.php modified
5+
@@ -13,10 +13,11 @@
6+
}
7+
session_write_close();
68
}
79

810
$arrWhitelist = [
911
+ '/webGui/images/partner-logo.svg',
1012
'/webGui/styles/clear-sans-bold-italic.eot',
1113
'/webGui/styles/clear-sans-bold-italic.woff',
1214
'/webGui/styles/clear-sans-bold-italic.ttf',
15+
'/webGui/styles/clear-sans-bold-italic.svg',
16+
'/webGui/styles/clear-sans-bold.eot',

api/src/unraid-api/unraid-file-modifier/modifications/patches/default-page-layout.patch

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
Index: /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php
22
===================================================================
3-
--- /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php
4-
+++ /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php
5-
@@ -557,14 +557,5 @@
3+
--- /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php original
4+
+++ /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php modified
5+
@@ -554,20 +554,11 @@
6+
return 'three';
7+
}
8+
function openNotifier() {
69
$.post('/webGui/include/Notify.php',{cmd:'get',csrf_token:csrf_token},function(msg) {
710
$.each($.parseJSON(msg), function(i, notify){
811
- $.jGrowl(notify.subject+'<br>'+notify.description,{
@@ -18,15 +21,27 @@ Index: /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php
1821
+
1922
});
2023
});
21-
@@ -698,6 +689,6 @@
24+
}
25+
function closeNotifier() {
26+
$.post('/webGui/include/Notify.php',{cmd:'get',csrf_token:csrf_token},function(msg) {
27+
@@ -695,12 +686,12 @@
28+
}
29+
// create list of nchan scripts to be started
30+
if (isset($button['Nchan'])) nchan_merge($button['root'], $button['Nchan']);
2231
}
2332

2433
-echo "<div class='nav-user show'><a id='board' href='#' class='hand'><b id='bell' class='icon-u-bell system'></b></a></div>";
2534

2635
+
2736
if ($themes2) echo "</div>";
2837
echo "</div></div>";
29-
@@ -904,20 +895,12 @@
38+
foreach ($buttons as $button) {
39+
annotate($button['file']);
40+
// include page specific stylesheets (if existing)
41+
@@ -901,26 +892,18 @@
42+
case 'warning': bell2++; break;
43+
case 'normal' : bell3++; break;
44+
}
3045
<?if ($notify['display']==0):?>
3146
if (notify.show) {
3247
- $.jGrowl(notify.subject+'<br>'+notify.description,{
@@ -52,7 +67,13 @@ Index: /usr/local/emhttp/plugins/dynamix/include/DefaultPageLayout.php
5267
+
5368
break;
5469
}
55-
@@ -1222,4 +1205,5 @@
70+
});
71+
72+
var nchan_plugins = new NchanSubscriber('/sub/plugins',{subscriber:'websocket'});
73+
@@ -1219,7 +1202,8 @@
74+
});
75+
}
76+
}
5677
});
5778
</script>
5879
+<unraid-toaster rich-colors close-button position="<?= ($notify['position'] === 'center') ? 'top-center' : $notify['position'] ?>"></unraid-toaster>

api/src/unraid-api/unraid-file-modifier/modifications/patches/log-rotate.patch

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Index: /etc/logrotate.d/unraid-api
22
===================================================================
3-
--- /etc/logrotate.d/unraid-api
4-
+++ /etc/logrotate.d/unraid-api
3+
--- /etc/logrotate.d/unraid-api original
4+
+++ /etc/logrotate.d/unraid-api modified
55
@@ -0,0 +1,12 @@
66
+
77
+/var/log/unraid-api/*.log {

api/src/unraid-api/unraid-file-modifier/modifications/patches/notifications-page.patch

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
Index: /usr/local/emhttp/plugins/dynamix/Notifications.page
22
===================================================================
3-
--- /usr/local/emhttp/plugins/dynamix/Notifications.page
4-
+++ /usr/local/emhttp/plugins/dynamix/Notifications.page
5-
@@ -135,23 +135,7 @@
3+
--- /usr/local/emhttp/plugins/dynamix/Notifications.page original
4+
+++ /usr/local/emhttp/plugins/dynamix/Notifications.page modified
5+
@@ -133,27 +133,11 @@
6+
_(Auto-close)_ (_(seconds)_):
7+
: <input type="number" name="life" class="a" min="0" max="60" value="<?=$notify['life']?>"> _(a value of zero means no automatic closure)_
68

79
:notifications_auto_close_help:
810

@@ -26,3 +28,5 @@ Index: /usr/local/emhttp/plugins/dynamix/Notifications.page
2628
_(Store notifications to flash)_:
2729
: <select name="path" class="a">
2830
<?=mk_option($notify['path'], "/tmp/notifications", _("No"))?>
31+
<?=mk_option($notify['path'], "/boot/config/plugins/dynamix/notifications", _("Yes"))?>
32+
</select>

api/src/unraid-api/unraid-file-modifier/modifications/patches/sso.patch

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Index: /usr/local/emhttp/plugins/dynamix/include/.login.php
22
===================================================================
33
--- /usr/local/emhttp/plugins/dynamix/include/.login.php original
44
+++ /usr/local/emhttp/plugins/dynamix/include/.login.php modified
5-
@@ -1,5 +1,33 @@
5+
@@ -1,6 +1,34 @@
66
<?php
77
+
88
+
@@ -36,7 +36,9 @@ Index: /usr/local/emhttp/plugins/dynamix/include/.login.php
3636

3737
// Only start a session to check if they have a cookie that looks like our session
3838
$server_name = strtok($_SERVER['HTTP_HOST'],":");
39-
@@ -203,9 +231,9 @@
39+
if (!empty($_COOKIE['unraid_'.md5($server_name)])) {
40+
@@ -202,11 +230,11 @@
41+
if ($failCount == $maxFails) my_logger("Ignoring login attempts for {$username} from {$remote_addr}");
4042
throw new Exception(_('Too many invalid login attempts'));
4143
}
4244

@@ -47,7 +49,9 @@ Index: /usr/local/emhttp/plugins/dynamix/include/.login.php
4749
// Bail if we need a token but it's invalid
4850
if (isWildcardCert() && $twoFactorRequired && !verifyTwoFactorToken($username, $token)) throw new Exception(_('Invalid 2FA token'));
4951

50-
@@ -537,8 +565,9 @@
52+
// Successful login, start session
53+
@@ -536,10 +564,11 @@
54+
document.body.textContent = '';
5155
document.body.appendChild(errorElement);
5256
}
5357
</script>
@@ -57,3 +61,4 @@ Index: /usr/local/emhttp/plugins/dynamix/include/.login.php
5761
<? if (($twoFactorRequired && !empty($token)) || !$twoFactorRequired) { ?>
5862
<div class="js-addTimeout hidden">
5963
<p class="error" style="padding-top:10px;"><?=_('Transparent 2FA Token timed out')?></p>
64+
<a href="https://forums.unraid.net/my-servers/" class="button button--small" title="<?=_('Go to My Servers Dashboard')?>"><?=_('Go to My Servers Dashboard')?></a>

0 commit comments

Comments
 (0)