Esempio n. 1
0
        public async Task <Guid> CreateFileAsync(CreateMediaItemServiceModel serviceModel)
        {
            var checksum = this.checksumService.GetMd5(serviceModel.File);

            var existingMediaItemVersion = context.MediaItemVersions.FirstOrDefault(x => x.Checksum == checksum && x.Filename == Path.GetFileNameWithoutExtension(serviceModel.File.FileName) && x.IsActive);

            if (existingMediaItemVersion != null)
            {
                return(existingMediaItemVersion.MediaItemId);
            }

            var mediaItem = new MediaItem
            {
                OrganisationId = serviceModel.OrganisationId,
                IsProtected    = false
            };

            context.MediaItems.Add(mediaItem.FillCommonProperties());

            var mediaItemVersion = new MediaItemVersion
            {
                MediaItemId = mediaItem.Id,
                Filename    = Path.GetFileNameWithoutExtension(serviceModel.File.FileName),
                Extension   = Path.GetExtension(serviceModel.File.FileName),
                Folder      = serviceModel.OrganisationId.ToString(),
                MimeType    = MimeUtility.GetMimeMapping(Path.GetExtension(serviceModel.File.FileName)),
                Size        = serviceModel.File.Length,
                Checksum    = checksum,
                CreatedBy   = serviceModel.Username,
                Version     = 1
            };

            context.MediaItemVersions.Add(mediaItemVersion.FillCommonProperties());

            var mediaItemTranslation = new MediaItemTranslation
            {
                MediaItemVersionId = mediaItemVersion.Id,
                Language           = serviceModel.Language,
                Name = Path.GetFileNameWithoutExtension(serviceModel.File.FileName)
            };

            context.MediaItemTranslations.Add(mediaItemTranslation.FillCommonProperties());

            context.SaveChanges();

            await this.mediaRepository.CreateFileAsync(mediaItemVersion.Id, serviceModel.OrganisationId.ToString(), serviceModel.File, serviceModel.File.FileName);

            return(mediaItem.Id);
        }
Esempio n. 2
0
        private static void SeedMedia(MediaContext context, IChecksumService checksumService, string storageConnectionString, Guid mediaId, Guid mediaVersionId, string mediaUrl)
        {
            if (!context.MediaItems.Any(x => x.Id == mediaId))
            {
                var container = new BlobContainerClient(storageConnectionString, MediaConstants.General.ContainerName);

                container.CreateIfNotExists();

                var blob = container.GetBlobClient($"{mediaVersionId}{Path.GetExtension(mediaUrl)}");

                using (var stream = new MemoryStream(File.ReadAllBytes(mediaUrl)))
                {
                    if (!blob.Exists())
                    {
                        blob.Upload(stream);
                    }

                    var mediaItem = new MediaItem
                    {
                        Id          = mediaId,
                        IsProtected = false
                    };

                    context.MediaItems.Add(mediaItem.FillCommonProperties());

                    var mediaItemVersion = new MediaItemVersion
                    {
                        Id          = mediaVersionId,
                        MediaItemId = mediaId,
                        Checksum    = checksumService.GetMd5(stream),
                        Filename    = Path.GetFileNameWithoutExtension(mediaUrl),
                        Extension   = Path.GetExtension(mediaUrl),
                        Folder      = MediaConstants.General.ContainerName,
                        MimeType    = MimeUtility.GetMimeMapping(Path.GetExtension(mediaUrl)),
                        Size        = blob.GetProperties().Value.ContentLength,
                        CreatedBy   = "system",
                        Version     = 1
                    };

                    context.MediaItemVersions.Add(mediaItemVersion.FillCommonProperties());

                    context.SaveChanges();
                }
            }
        }