summaryrefslogtreecommitdiff
path: root/Pages/ViewMedia.razor
blob: e8914a08184172be0a6d24c9af8d595f06f681d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
@page "/ViewMedia"
@using HyperBooru.Util
@inject IJSRuntime jsRuntime
@inject IDbContextFactory<HBContext> dbFactory
@inject ITagService tagService
@inject IMediaService mediaService
@implements IDisposable

<PageTitle>@title</PageTitle>

<script suppress-warning="BL9992">
    function toggleSidebar() {
        document.getElementById("metadata").classList.toggle("hidden");
    }
</script>

<div id="content">
    <div id="image-container">
        <img
            src="/media/@(media.Guid)"
            width=@media.CurrentUploadedFile.Width
            height=@media.CurrentUploadedFile.Height
            onclick="toggleSidebar()"/>
    </div>
    <div id="metadata">
        <div id="metadata-container">
            <div id="metadata-fileinfo">
                @if(infoEditMode) {
                    <form @onsubmit=@(() => ApplyInfoEdit(true))>
                        <table id="edit-metadata">
                            <tr>
                                <td>Title:</td>
                                <td><input type="text" @bind=shortDescription/></td>
                            </tr>
                            <tr>
                                <td>Description:</td>
                                <td><textarea rows="4" @bind=longDescription/></td>
                            </tr>
                        </table>
                    </form>
                } else {
                    <p>Title: <i>@(media.ShortDescription ?? "None")</i></p>
                    <p class="newlines">Description:<br/><i>@(media.LongDescription ?? "None")</i></p>
                }
                <p>Resolution: @(media.CurrentUploadedFile.Width)x@(media.CurrentUploadedFile.Height)</p>
                <p class="heading">Upload history</p>
                <hr/>
                <table id="uploaded-files" class="data-table">
                    <tr>
                        <th>Created On</th>
                        <th>Last Write</th>
                        <th>Uploaded On</th>
                        <th>Filename</th>
                        <th>Size</th>
                        <th>Original Checksum</th>
                    </tr>
                    @foreach(var file in media.UploadedFiles) {
                        <tr>
                            <td title=@file.CreateTime?.ToString()>
                                @(file.CreateTime?.ToString("d") ?? "N/A")
                            </td>
                            <td title=@file.LastWriteTime?.ToString()>
                                @(file.LastWriteTime?.ToString("d") ?? "N/A")
                            </td>
                            <td title=@file.UploadTime>@(file.UploadTime.ToString("d"))</td>
                            <td title=@file.Filename>@file.Filename</td>
                            <td title=@file.Length>@file.Length.ToBytesSI()</td>
                            <td
                                title=@(file.Checksum + (file.ChecksumVerified ? " (verified)" : ""))
                                class=@(file.ChecksumVerified ? "verified" : null)>
                                
                                @file.Checksum.Substring(0, 8)
                            </td>
                        </tr>
                    }
                </table>
            </div>
            <div id="metadata-tags">
                <p class="heading">Tags</p>
                <hr/>
                <MediaTagTable Media=media @ref=mediaTagTable/>
            </div>
        </div>
        <div id="button-container">
            <ButtonContainer>
                <button @onclick=@(() => deleteDialog.Show()) class="warning">Delete</button>
                <button @onclick=@(() => tagDialog.Show()) class="secondary">Add Tag</button>
                <button @onclick=@(() => ocrDialog.Show()) class="secondary">View OCR</button>
                @if(infoEditMode) {
                    <button @onclick=@(() => ApplyInfoEdit(false)) class="secondary">Cancel</button>
                    <button @onclick=@(() => ApplyInfoEdit(true))>Apply</button>
                } else {
                    <button @onclick=@(() => InfoEditMode = true) class="secondary">Edit Info</button>
                }
                @if(media.IsIngest) {
                    <button @onclick=@(() => SetIngest(false))>Mark Tagging Complete</button>
                } else {
                    <button class="secondary" @onclick=@(() => SetIngest(true))>Mark Tagging Incomplete</button>
                }
            </ButtonContainer>
        </div>
    </div>
</div>

<Dialog Title="Delete this media?" @ref=deleteDialog>
    <ButtonContainer>
        <button @onclick=@(() => deleteDialog.Hide()) class="secondary">Cancel</button>
        <button @onclick=DeleteMedia class="warning">Confirm</button>
    </ButtonContainer>
</Dialog>

<Dialog Title="OCR Data" @ref=ocrDialog>
    @if(media.OcrData is null) {
        <p><center>This media item hasn't been scanned yet!</center></p>
    } else {
        <code style="max-height:400px;">@media.OcrData?.Text</code>
    }
    <ButtonContainer>
        <button @onclick=@(() => ocrDialog.Hide())>Close</button>
    </ButtonContainer>
</Dialog>

<TagSelectDialog
    Title="Select one or more tag(s) to add"
    OnSubmit=AddTags
    @ref=tagDialog/>

@code {
    [Parameter]
    [SupplyParameterFromQuery(Name = "m")]
    public Guid MediaId { get; set; }

    private Media media;

    private string title;

    private bool    infoEditMode = false;
    private string? shortDescription;
    private string? longDescription;

    private MediaTagTable   mediaTagTable;
    private Dialog          deleteDialog;
    private Dialog          ocrDialog;
    private TagSelectDialog tagDialog;

    private HBContext db;

    protected override void OnInitialized() {
        db = dbFactory.CreateDbContext();
        LoadMedia();
    }

    private void LoadMedia() {
        media = db.Media
            .Include(m => m.Tags)
            .ThenInclude(t => t.TagDefinition)
            .Include(m => m.CurrentUploadedFile)
            .Include(m => m.UploadedFiles)
            .Include(m => m.OcrData)
            .First(m => m.Guid == MediaId);

        title = media.DisplayName ?? "Media View";
    }

    private void AddTags(TagDefinition[] tagDefs) {
        foreach(var tagDef in tagDefs)
            tagService.AddTag(media, tagDef);
        mediaTagTable.Refresh();
    }

    private async void SetIngest(bool ingest) {
        mediaService.SetIngest(media, ingest);
        db.Entry(media).State = EntityState.Detached;
        LoadMedia();

        if(ingest)
            StateHasChanged();
        else
            await jsRuntime.InvokeVoidAsync("history.back");
    }

    private bool InfoEditMode {
        get => infoEditMode;
        set {
            shortDescription = media.ShortDescription;
            longDescription  = media.LongDescription;
            infoEditMode     = value;
            StateHasChanged();
        }
    }

    private void ApplyInfoEdit(bool apply) {
        if(apply) {
            if(string.IsNullOrEmpty(shortDescription)) shortDescription = null;
            if(string.IsNullOrEmpty(longDescription))  longDescription  = null;
            media.ShortDescription = shortDescription;
            media.LongDescription  = longDescription;
            mediaService.SetDescription(media, shortDescription, longDescription);
        }

        infoEditMode = false;
    }

    private async void DeleteMedia() {
        mediaService.Delete(media);
        await jsRuntime.InvokeVoidAsync("history.back");
    }

    public void Dispose() => db.Dispose();
}