-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAdminController.cs
391 lines (346 loc) · 15.2 KB
/
AdminController.cs
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
using ClassTranscribeDatabase;
using ClassTranscribeDatabase.Models;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json.Linq;
using System;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
namespace ClassTranscribeServer.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AdminController : BaseController
{
private readonly WakeDownloader _wakeDownloader;
private readonly IAuthorizationService _authorizationService;
public AdminController(IAuthorizationService authorizationService, WakeDownloader wakeDownloader,
CTDbContext context, ILogger<AdminController> logger) : base(context, logger)
{
_authorizationService = authorizationService;
_wakeDownloader = wakeDownloader;
}
[HttpPost("UpdateOffering")]
public async Task<ActionResult> UpdateOffering(string offeringId)
{
var offering = await _context.Offerings.FindAsync(offeringId);
if (offering == null)
{
return BadRequest();
}
var authorizationResult = await _authorizationService.AuthorizeAsync(this.User, offering, Globals.POLICY_UPDATE_OFFERING);
if (!authorizationResult.Succeeded)
{
if (User.Identity.IsAuthenticated)
{
return new ForbidResult();
}
return new ChallengeResult();
}
_wakeDownloader.UpdateOffering(offeringId);
return Ok();
}
/// <summary>
/// Enqueue DownloadAllPlaylists task, which updates all playlists for all terms where start date is within 6 months of today.
///
/// </summary>
/// <remarks>
/// Each playlist update is a separate task. Requesting an update is harmless though
/// be aware that some external sources (e.g. Youtube) limit API usage.
/// See QueueAwakerTask.DownloadAllPlaylists, DownloadPlaylistInfoTask for details
/// This API call is just for the impatient because the PeriodicCheck task also updates
/// all playlists and (unlike this API function) also performs a PendingJobs task to kick off transcriptions.
/// </remarks>
[HttpPost("UpdateAllPlaylists")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult UpdateAllPlaylists()
{
_wakeDownloader.UpdateAllPlaylists();
return Ok();
}
/// <summary>
/// Regenerate one Caption (vtt, srt) file of the given Transcription
/// </summary>
/// will be deleted soon - We now generate vtt files dynamically.
// [HttpPost("UpdateVTTFile")]
// [Authorize(Roles = Globals.ROLE_ADMIN)]
// public ActionResult UpdateVTTFile(string transcriptionId)
// {
// _logger.LogInformation($"Enqueueing {transcriptionId} caption regeneration");
// _wakeDownloader.UpdateVTTFile(transcriptionId);
// return Ok();
// }
/// <summary>
/// Regenerate all Caption (vtt, srt) files of the given course offering
/// </summary>
/// Will be deleted soon - we no longer store vtt files
// [HttpPost("UpdateVTTFilesInCourseOffering")]
// [Authorize(Roles = Globals.ROLE_ADMIN)]
// public async Task<ActionResult> UpdateVTTFilesInCourseOffering(string offeringId = null)
// {
// var playlistIds = await _context.Playlists.Where(p => p.OfferingId == offeringId).Select(p => p.Id).ToListAsync();
// _logger.LogInformation($"UpdateVTTFilesinPlaylist(${offeringId}): Found {playlistIds.Count} playlists");
// var videoIds = await _context.Medias.Where(m => playlistIds.Contains(m.PlaylistId)).Select(m => m.VideoId).ToListAsync();
// _logger.LogInformation($"UpdateVTTFilesinPlaylist(): Found {videoIds.Count} videos");
// var transcriptionIds = await _context.Transcriptions.Where(t => videoIds.Contains(t.VideoId)).Select(t => t.Id).ToListAsync();
// _logger.LogInformation($"UpdateVTTFilesinPlaylist(): Found {transcriptionIds.Count} vtt transcriptions to regenerate");
// foreach (var t in transcriptionIds)
// {
// _wakeDownloader.UpdateVTTFile(t);
// }
// return Ok($"Requested {transcriptionIds.Count} Transcriptions to be regenerated from {videoIds.Count} videos in {playlistIds.Count} playlists");
// }
/// <summary>
/// Regenerate all Caption (vtt, srt) files of all transcriptions
/// </summary>
/// will be deleted soon - we no longer store vtt files
// [HttpPost("UpdateAllVTTFiles")]
// [Authorize(Roles = Globals.ROLE_ADMIN)]
// public async Task<ActionResult> UpdateAllVTTFiles()
// {
// var transcriptionIds = await _context.Transcriptions.Select(t => t.Id).ToListAsync();
// _logger.LogInformation($"UpdateAllVTTFiles: Enqueueing {transcriptionIds.Count} vtt transcriptions to regenerate");
// foreach (var t in transcriptionIds)
// {
// _wakeDownloader.UpdateVTTFile(t);
// }
// return Ok();
// }
/// <summary>
/// Enqueue DownloadPlaylist task, which updates one playlist.
/// </summary>
/// <remarks>
/// Requesting an update is harmless though
/// be aware that some external sources (e.g. Youtube) limit API usage.
/// See QueueAwakerTask.DownloadAllPlaylists, DownloadPlaylistInfoTask for details
/// This API call is just for the impatient because the PeriodicCheck task also updates
/// all playlists and (unlike this API function) also performs a PendingJobs task to kick off transcriptions.
/// </remarks>
[HttpPost("UpdatePlaylist")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult UpdatePlaylist(string playlistId)
{
_wakeDownloader.UpdatePlaylist(playlistId);
return Ok();
}
[HttpPost("DescribeVideo")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult DescribeVideo(string playlistMediaVideoId, bool deleteExisting)
{
_wakeDownloader.DescribeVideo(playlistMediaVideoId, deleteExisting);
return Ok();
}
/// <summary>
/// Requests a re-download of missing media
/// </summary>
/// <remarks>
/// Enqueues a DownloadMedia task. Requests missing media (as opposed to waiting for the periodic check to discover them)
///
/// Duplicates are discarded. New videos cause captions and video processing tasks to be requested
/// See DownloadMediaTask.cs for more details.
/// </remarks>
[HttpPost("DownloadMedia")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult DownloadMedia(string mediaId)
{
_wakeDownloader.DownloadMedia(mediaId);
return Ok();
}
/// <sumarize>
/// Enqueue a ConvertMedia task. This creates a wav file (no longer used) and request captions
/// </sumarize>
/// <remarks>
/// It is unclear if this request is still useful.
/// </remarks>
[HttpPost("ConvertMedia")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult ConvertMedia(string videoId)
{
_wakeDownloader.ConvertMedia(videoId);
return Ok();
}
/// <summary>
///
/// </summary>
/// <param name="videoOrMediaId">A videoId or mediaId</param>
/// <param name="deleteExisting">If true, existing transriptions are deleted first</param>
/// <returns></returns>
[HttpPost("TranscribeVideo")]
public ActionResult TranscribeVideo(string videoOrMediaId, bool deleteExisting)
{
_wakeDownloader.TranscribeVideo(videoOrMediaId, deleteExisting);
return Ok();
}
[HttpPost("ReTranscribePlaylist")]
public ActionResult ReTranscribePlaylist(string playlistId)
{
_wakeDownloader.ReTranscribePlaylist(playlistId);
return Ok();
}
[HttpPost("SceneDetectVideo")]
public ActionResult SceneDetectVideo(string videoMediaPlaylistId, bool deleteExisting)
{
_wakeDownloader.SceneDetection(videoMediaPlaylistId, deleteExisting);
return Ok();
}
[HttpPost("UpdatePhraseHintsSchema")]
public async Task<ActionResult<int>> UpdatePhraseHintsSchema(String videoId)
{
var videosToUpdate= (videoId == "all") ? _context.Videos.Where(v=>v.PhraseHints.Length>0).Take(1000) : _context.Videos.Where(v=> v.Id == videoId);
int count = 0;
foreach (var video in videosToUpdate) {
count ++;
_logger.LogInformation($"{count}: UpdatePhraseHintsSchema {video.Id}");
var hints = video.PhraseHints;
if(video.HasPhraseHints()) {
_logger.LogInformation($"UpdatePhraseHintsSchema {video.Id} - already has Phrase Hints - Skipping");
continue;
} else {
TextData data = new TextData();
data.Text = hints;
_context.TextData.Add(data);
video.PhraseHintsDataId = data.Id;
Trace.Assert(!string.IsNullOrEmpty(data.Id));
video.PhraseHints = null;
}
}
await _context.SaveChangesAsync();
return count;
}
[HttpPost("UpdateSceneDataSchema")]
public async Task<ActionResult<int>> UpdateSceneDataSchema(String requestId)
{
string[] videoIdList = null;
if(requestId == "all") {
videoIdList = _context.Videos.Select(v=>v.Id).ToArray<string>();
}
else {
videoIdList = new string[] { requestId };
}
int count = 0;
var empty = JObject.Parse("{}");
foreach (var id in videoIdList) {
var video = await _context.Videos.FindAsync(id);
count ++;
_logger.LogInformation($"{count}: UpdateSceneDataSchema {video.Id}");
if(video.HasSceneObjectData()) {
_logger.LogInformation($"UpdateSceneDataSchema {video.Id} - already has SceneOjectData - Skipping");
continue;
} else {
JToken olddata = video.SceneData;
TextData data = new TextData();
data.SetFromJSON(olddata);
_context.TextData.Add(data);
video.SceneObjectDataId = data.Id;
System.Diagnostics.Trace.Assert(!string.IsNullOrEmpty(data.Id));
}
video.SceneData = empty;
await _context.SaveChangesAsync();
}
return count;
}
[HttpPost("UpdateASLVideos")]
public ActionResult UpdateASL(string sourceId)
{
_wakeDownloader.UpdateASLVideo(sourceId);
return Ok();
}
[HttpPost("PeriodicCheck")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public ActionResult PeriodicCheck()
{
_wakeDownloader.PeriodicCheck();
return Ok();
}
[HttpGet("CreateBoxToken")]
[AllowAnonymous]
public ActionResult CreateBoxToken([FromQuery] string code)
{
_wakeDownloader.CreateBoxToken(code);
return Ok("Request made to createBoxToken.");
}
/// <summary>
/// Returns the sha1 commit hash and build number, or 'unspecified' if these are unknown
/// Example result : {"Commit":"hexadecimalnumber","Build":"123"}
/// </summary>
[HttpGet("GetVersion")]
[AllowAnonymous]
[Produces("application/json")]
#pragma warning disable CA1822 // The warning suggests marking this as static but ASP.NET doesn't support static endpoints
public ActionResult<BuildVersionDTO> GetVersion()
#pragma warning restore CA1822
{
BuildVersionDTO result = new BuildVersionDTO()
{
Commit = Globals.appSettings.GITSHA1,
Build = Globals.appSettings.BUILDNUMBER
};
return result;
}
/// <summary>
/// Attempts to generate FilePath fields for all Course and CourseOffering entities that currently
/// do not have FilePath fields. This also creates the corresponding directories.
///
/// Return the number of successfully generated file paths.
/// </summary>
[HttpPost("GenerateFilePaths")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public async Task<ActionResult<int>> GenerateFilePaths()
{
int numGenerated = 0;
var courses = await _context.Courses
.Where(c => string.IsNullOrEmpty(c.FilePath))
.ToListAsync();
foreach (var c in courses) {
try
{
await FileRecord.SetFilePath(_context, c);
numGenerated++;
}
catch (InvalidOperationException) { }
};
var courseOfferings = await _context.CourseOfferings
.Where(co => string.IsNullOrEmpty(co.FilePath))
.ToListAsync();
foreach (var co in courseOfferings)
{
try
{
await FileRecord.SetFilePath(_context, co);
numGenerated++;
}
catch (InvalidOperationException) { }
}
return numGenerated;
}
[HttpGet("MediaModel/{mediaOrVideoId}")]
[Authorize(Roles = Globals.ROLE_ADMIN)]
public async Task<ActionResult<object>> GetObject(string objectId) {
Course c = await _context.Courses.FindAsync(objectId);
if(c != null) return c;
Offering o = await _context.Offerings.FindAsync(objectId);
if(o != null) return o;
Playlist p = await _context.Playlists.FindAsync(objectId);
if(p != null) return p;
Media m = await _context.Medias.FindAsync(objectId);
if(m != null) return m;
Video v = await _context.Videos.FindAsync(objectId);
if(v != null) return v;
Transcription t = await _context.Transcriptions.FindAsync(objectId);
if(t != null) return t;
Caption cap = await _context.Captions.FindAsync(objectId);
if(cap != null) return cap;
EPub epub = await _context.EPubs.FindAsync(objectId);
if(epub != null) return epub;
return NotFound();
}
public class BuildVersionDTO
{
public string Commit { get; set; }
public string Build { get; set; }
}
}
}