コード例 #1
0
            public async Task <string> AddInstructorAudioMedia(string instructorId, string firstName, string lastName, string title, int durationInMinutes, DateTime recordingDate, DateTime uploadDate, byte[] streamedFileContent)
            {
                // TODO: calculate this some way automatically... someday
                string fileExtension = "m4a";

                // Generate Container Name - InstructorSpecific
                string containerName = $"{firstName[0].ToString().ToLower()}{lastName.ToLower()}-{instructorId}";

                // TODO: calculate this some way automatically... someday
                string contentType = "audio/mp4";
                // TODO: calculate this some way automatically... someday
                FileType fileType = FileType.audio;

                string authorName      = $"{firstName} {lastName}";
                string authorShortName = $"{firstName[0]}{lastName}";
                string description     = $"{authorShortName} - {title}";

                // TODO: Get this from FFMPEG... someday
                long duration = (durationInMinutes * 60000);

                // Generate new filename
                string fileName = $"{firstName[0].ToString().ToLower()}{lastName.ToLower()}-{Guid.NewGuid()}";

                // TODO: Move this to a BlobCreatedResponse object
                BlobCreatedEvent blobCreatedResult;

                try
                {
                    // Update file properties in storage
                    Dictionary <string, string> fileProperties = new Dictionary <string, string>();
                    fileProperties.Add("ContentType", contentType);

                    // update file metadata in storage
                    Dictionary <string, string> metadata = new Dictionary <string, string>();
                    metadata.Add("author", authorShortName);
                    metadata.Add("tite", title);
                    metadata.Add("description", description);
                    metadata.Add("duration", duration.ToString());
                    metadata.Add("recordingDate", recordingDate.ToString());
                    metadata.Add("uploadDate", uploadDate.ToString());

                    var fileNameWExt = $"{fileName}.{fileExtension}";

                    blobCreatedResult = await _cloudStorageService.CreateBlob(containerName, fileNameWExt, streamedFileContent, "audio", fileProperties, metadata);
                }
                catch (StorageException e)
                {
                    throw e;
                }

                Model.Media media = Model.Media.Create(string.Empty, instructorId, authorName, fileName, fileType, fileExtension, recordingDate, uploadDate, ContentDetails.Create(title, description, duration, blobCreatedResult.Size, 0, new List <string>()), StateDetails.Create(StatusType.STAGED, DateTime.MinValue, DateTime.UtcNow, DateTime.MaxValue), Manifest.Create(new Dictionary <string, string>()));

                // upload to MongoDB
                if (!blobCreatedResult.Error)
                {
                    var mapper = new Mapper(_mapperConfiguration);

                    var dao = mapper.Map <ContentDAO>(media);

                    try
                    {
                        await _db.Content.InsertOneAsync(dao);
                    }
                    catch (Exception)
                    {
                        return(string.Empty);
                    }

                    return(dao.Id.ToString());
                }
                else
                {
                    // metadata wasn't stored, remove blob
                    await _cloudStorageService.DeleteBlob(containerName, fileName, "audio");

                    throw new AddMediaException($"An issue occurred during media upload: rolling back storage change");
                }
            }