public async override Task <TranscriptSet> QueryOneAsync(FileIdentifier identifier, CancellationToken cancellationToken = default(CancellationToken)) { var rootFile = await Connection.File.GetAsync(identifier); var transcriptJson = rootFile.Read <List <AlternativeView> >(MetadataKeyConstants.ALTERNATIVE_VIEWS) .FirstOrDefault(v => v.Name == FILENAME_JSON); var set = await ViewSetService.LoadSet <TranscriptSet>(identifier); if (transcriptJson != null) { set.Segments = await Connection.File.DownloadAsAsync <List <SegmentModel> >(transcriptJson.FileIdentifier); } else { var vttIdentifier = set.Subtitles?.FirstOrDefault()?.FileIdentifier; set.Segments = await TranscriptService.LoadSegments(Connection, vttIdentifier); } return(set); }
public async override Task <ClipSet> QueryOneAsync(FileIdentifier identifier, CancellationToken cancellationToken = default(CancellationToken)) { var rootFile = await Connection.File.GetAsync(identifier); var clipSetJson = rootFile.Read <List <AlternativeView> >(MetadataKeyConstants.ALTERNATIVE_VIEWS) ?.FirstOrDefault(v => v.Name == FILENAME); var set = await ViewSetService.LoadSet <ClipSet>(identifier); if (clipSetJson != null) { set.Segments = await Connection.File.DownloadAsAsync <List <ClipSegmentModel> >(clipSetJson.FileIdentifier); } var ops = set.AllowedOperations.ToList(); ops.Add(AllowedOperation.GetAllowedOperationExportFrame(identifier)); ops.Add(AllowedOperation.GetAllowedOperationExportClip(identifier)); set.AllowedOperations = ops; return(set); }
public async Task DownloadZip( string fileName, IEnumerable <FileIdentifier> fileIdentifiers, HttpResponse response, Func <FileModel, PathIdentifier> pathGenerator = null, CancellationToken cancellationToken = default(CancellationToken) ) { if (pathGenerator == null) { pathGenerator = APIModelExtensions.MetaPathIdentifierRead; } response.StatusCode = 200; response.ContentType = "application/zip"; var contentDisposition = new ContentDispositionHeaderValue("attachment"); contentDisposition.SetHttpFileName(fileName); response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString(); var isEDiscoveryUser = EDiscoveryUtility.IsUserEDiscovery(Connection.UserAccessIdentifiers); OrganizationModel organization = null; using (var archive = new ZipArchive(response.Body, ZipArchiveMode.Create)) { foreach (var fileIdentifier in fileIdentifiers) { if (organization == null || organization.Identifier.OrganizationKey != fileIdentifier.OrganizationKey) { organization = await Connection.Organization.GetAsync(fileIdentifier as OrganizationIdentifier); } var fileModel = await Connection.File.GetAsync(fileIdentifier); var views = ViewSetService.DetectFileViews(organization, fileModel); var newFileExtension = null as string; var contentsFileIdentifier = fileIdentifier; if (views.Any()) { var first = views.First(); switch (first.ViewerType) { case ManagerFileView.ViewerTypeEnum.Video: var mediaSet = await viewSetService.LoadSet <MediaSet>(fileIdentifier); var mp4 = mediaSet.Sources.FirstOrDefault(s => s.Type == "video/mp4"); if (mp4 != null) { newFileExtension = "mp4"; contentsFileIdentifier = mp4.FileIdentifier; } break; case ManagerFileView.ViewerTypeEnum.Document: if (fileModel.Extension == "docx") { var documentSet = await viewSetService.LoadSet <DocumentSet>(fileIdentifier); var pdf = documentSet.FileIdentifier; if (pdf != null) { newFileExtension = "pdf"; contentsFileIdentifier = pdf; } } break; } } var path = pathGenerator(fileModel); string name = fileModel.Name; if (newFileExtension != null) { name = $"{Path.GetFileNameWithoutExtension(name)}.{newFileExtension}"; } else { newFileExtension = fileModel.Extension; } name = MakeFilenameSafe(name); var filename = path != null ? Path.Combine(path.PathKey, name) : name; var entry = archive.CreateEntry(filename); using (var fileStream = entry.Open()) { await Connection.File.DownloadAsync( contentsFileIdentifier, onStreamAvailable : async(stream, cancel) => { if (isEDiscoveryUser && WatermarkFilter.IsSupportedFileType(newFileExtension)) { var packageName = fileModel.Read <string>( MetadataKeyConstants.E_DISCOVERY_SHARE_PACKGAGE); var watermarkUser = await Connection.User.GetAsync(Connection.UserIdentifier); // this code is redundant with FileService.ExecuteDownload await WatermarkFilter.Execute(stream, fileStream, newFileExtension, $"Defense {packageName} {watermarkUser.EmailAddress}" ); } else { await stream.CopyToAsync(fileStream); } }, cancellationToken : cancellationToken ); } } } }