private void MoveMedia() { if (!_config.StoreMediaInDB) { // All pictures have initially been stored in the DB. Move the binaries to disk. var fileSystemStorageProvider = new FileSystemMediaStorageProvider(new MediaFileSystem()); var mediaStorages = _ctx.Set <MediaStorage>(); using (var scope = new DbContextScope(ctx: _ctx, autoDetectChanges: true, autoCommit: false)) { var mediaFiles = _ctx.Set <MediaFile>() .Expand(x => x.MediaStorage) .Where(x => x.MediaStorageId != null) .ToList(); foreach (var mediaFile in mediaFiles) { if (mediaFile.MediaStorage?.Data?.LongLength > 0) { fileSystemStorageProvider.Save(mediaFile, MediaStorageItem.FromStream(mediaFile.MediaStorage.Data.ToStream())); mediaFile.MediaStorageId = null; mediaFile.MediaStorage = null; } } scope.Commit(); } } }
private async Task MoveMedia() { if (_config.StoreMediaInDB) { return; } // All pictures have initially been stored in the DB. Move the binaries to disk as configured. var fileSystemStorageProvider = EngineContext.Current.ResolveService <Func <IMediaStorageProvider> >().Invoke(); using (var scope = new DbContextScope(_db, autoDetectChanges: true)) { var mediaFiles = await _db.MediaFiles .Include(x => x.MediaStorage) .Where(x => x.MediaStorageId != null) .ToListAsync(); foreach (var mediaFile in mediaFiles) { if (mediaFile.MediaStorage?.Data?.LongLength > 0) { await fileSystemStorageProvider.SaveAsync(mediaFile, MediaStorageItem.FromStream(mediaFile.MediaStorage.Data.ToStream())); mediaFile.MediaStorageId = null; mediaFile.MediaStorage = null; } } await scope.CommitAsync(); } }
public async Task PublishAsync(ExportDeployment deployment, ExportDeploymentContext context, CancellationToken cancellationToken) { var emailAddresses = deployment.EmailAddresses .SplitSafe(",") .Where(x => x.IsEmail()) .ToArray(); if (!emailAddresses.Any()) { return; } var emailAccount = await _db.EmailAccounts.FindByIdAsync(deployment.EmailAccountId, false, cancellationToken); var fromEmailAddress = emailAccount.ToMailAddress(); var files = await context.GetDeploymentFilesAsync(cancellationToken); var canStreamBlob = _db.DataProvider.CanStreamBlob; var num = 0; foreach (var emailAddress in emailAddresses) { var queuedEmail = new QueuedEmail { From = fromEmailAddress, SendManually = false, To = emailAddress, Subject = deployment.EmailSubject.NaIfEmpty(), Body = deployment.EmailSubject.NaIfEmpty(), CreatedOnUtc = DateTime.UtcNow, EmailAccountId = deployment.EmailAccountId }; foreach (var file in files) { var name = file.Name; var attachment = new QueuedEmailAttachment { StorageLocation = EmailAttachmentStorageLocation.Blob, Name = name, MimeType = MimeTypes.MapNameToMimeType(name) }; using var item = MediaStorageItem.FromFile(file); await _dbMediaStorageProvider.ApplyBlobAsync(attachment, item, false); queuedEmail.Attachments.Add(attachment); } _db.QueuedEmails.Add(queuedEmail); // Blob data could be large, so better not bulk commit here. num += await _db.SaveChangesAsync(cancellationToken); } context.Log.Info($"{num} email(s) created and queued for deployment."); }
private void InternalCopyFileData(MediaFile file, MediaFile copy) { _storageProvider.Save(copy, MediaStorageItem.FromStream(_storageProvider.OpenRead(file))); _imageCache.Delete(copy); using (var scope = new DbContextScope(_fileRepo.Context, autoCommit: false)) { // Tags. _fileRepo.Context.LoadCollection(file, (MediaFile x) => x.Tags); var existingTagsIds = copy.Tags.Select(x => x.Id).ToList(); foreach (var tag in file.Tags) { if (!existingTagsIds.Contains(tag.Id)) { copy.Tags.Add(tag); existingTagsIds.Add(tag.Id); } } // Localized values. var languages = _languageService.GetAllLanguages(true); foreach (var language in languages) { var title = file.GetLocalized(x => x.Title, language.Id, false, false).Value; if (title.HasValue()) { _localizedEntityService.SaveLocalizedValue(copy, x => x.Title, title, language.Id); } var alt = file.GetLocalized(x => x.Alt, language.Id, false, false).Value; if (alt.HasValue()) { _localizedEntityService.SaveLocalizedValue(copy, x => x.Alt, alt, language.Id); } } scope.Commit(); _fileRepo.Context.DetachEntities <MediaTag>(); } }
private async Task InternalCopyFileData(MediaFile file, MediaFile copy) { await _storageProvider.SaveAsync(copy, MediaStorageItem.FromStream(await _storageProvider.OpenReadAsync(file))); await _imageCache.DeleteAsync(copy); // Tags. await _db.LoadCollectionAsync(file, (MediaFile x) => x.Tags); var existingTagsIds = copy.Tags.Select(x => x.Id).ToList(); foreach (var tag in file.Tags) { if (!existingTagsIds.Contains(tag.Id)) { copy.Tags.Add(tag); existingTagsIds.Add(tag.Id); } } // Localized values. var languages = _languageService.GetAllLanguages(true); foreach (var language in languages) { var title = file.GetLocalized(x => x.Title, language.Id, false, false).Value; if (title.HasValue()) { await _localizedEntityService.ApplyLocalizedValueAsync(copy, x => x.Title, title, language.Id); } var alt = file.GetLocalized(x => x.Alt, language.Id, false, false).Value; if (alt.HasValue()) { await _localizedEntityService.ApplyLocalizedValueAsync(copy, x => x.Alt, alt, language.Id); } } await _db.SaveChangesAsync(); _db.DetachEntities <MediaTag>(); }
protected async Task <(MediaStorageItem StorageItem, MediaFile File)> ProcessFile( MediaFile file, MediaPathData pathData, Stream inStream, bool isTransient = true, DuplicateFileHandling dupeFileHandling = DuplicateFileHandling.ThrowError, MimeValidationType mediaValidationType = MimeValidationType.MimeTypeMustMatch) { if (file != null) { if (dupeFileHandling == DuplicateFileHandling.ThrowError) { var fullPath = pathData.FullPath; await CheckUniqueFileName(pathData); throw _exceptionFactory.DuplicateFile(fullPath, ConvertMediaFile(file, pathData.Folder), pathData.FullPath); } else if (dupeFileHandling == DuplicateFileHandling.Rename) { if (await CheckUniqueFileName(pathData)) { file = null; } } } if (file != null && mediaValidationType != MimeValidationType.NoValidation) { if (mediaValidationType == MimeValidationType.MimeTypeMustMatch) { ValidateMimeTypes("Save", file.MimeType, pathData.MimeType); } else if (mediaValidationType == MimeValidationType.MediaTypeMustMatch) { ValidateMediaTypes("Save", _typeResolver.Resolve(pathData.Extension), file.MediaType); } // Restore file if soft-deleted file.Deleted = false; // Delete thumbnail await _imageCache.DeleteAsync(file); } file ??= new MediaFile { IsTransient = isTransient, FolderId = pathData.Node.Value.Id }; // Untrackable folders can never contain transient files. if (!pathData.Folder.CanDetectTracks) { file.IsTransient = false; } var name = pathData.FileName; if (name != pathData.FileName) { pathData.FileName = name; } file.Name = pathData.FileName; file.Extension = pathData.Extension; file.MimeType = pathData.MimeType; if (file.MediaType == null) { file.MediaType = _typeResolver.Resolve(pathData.Extension, pathData.MimeType); } // Process image if (inStream != null && inStream.Length > 0 && file.MediaType == MediaType.Image && (await ProcessImage(file, inStream)).Out(out var outImage)) { file.Width = outImage.Width; file.Height = outImage.Height; file.PixelSize = outImage.Width * outImage.Height; return(MediaStorageItem.FromImage(outImage), file); } else { file.RefreshMetadata(inStream, _imageProcessor.Factory); return(MediaStorageItem.FromStream(inStream), file); } }
public void MigrateUploadedFiles(SmartObjectContext ctx) { var fileSet = ctx.Set <MediaFile>(); var folderSet = ctx.Set <MediaFolder>(); using (var scope = new DbContextScope(ctx, hooksEnabled: false, autoCommit: false, validateOnSave: false, lazyLoading: false, autoDetectChanges: false)) { var albumId = _albumRegistry.GetAlbumByName(SystemAlbumProvider.Files)?.Id; var rootFolder = _mediaFileSystem.GetFolder("Uploaded"); if (!rootFolder.Exists) { return; } ProcessFolder(rootFolder, albumId.Value); void ProcessFolder(IFolder folder, int mediaFolderId) { var newFiles = new List <FilePair>(); foreach (var uploadedFile in _mediaFileSystem.ListFiles(folder.Path)) { var file = new MediaFile { CreatedOnUtc = uploadedFile.LastUpdated, UpdatedOnUtc = uploadedFile.LastUpdated, Extension = uploadedFile.Extension.TrimStart('.'), Name = uploadedFile.Name, MimeType = MimeTypes.MapNameToMimeType(uploadedFile.Name), Size = Convert.ToInt32(uploadedFile.Size), FolderId = mediaFolderId, Version = 2 }; ProcessMediaFile(file); newFiles.Add(new FilePair { MediaFile = file, UploadedFile = uploadedFile }); fileSet.Add(file); } // Process/save files of current folder try { // Save files to DB int num = scope.Commit(); // Copy/Move files foreach (var newFile in newFiles) { if (_isFsProvider) { var newPath = GetStoragePath(newFile.MediaFile); if (!_mediaFileSystem.FileExists(newPath)) { // TODO: (mm) (mc) should we actually MOVE the file? _mediaFileSystem.CopyFile(newFile.UploadedFile.Path, newPath); } } else { _mediaStorageProvider.Save(newFile.MediaFile, MediaStorageItem.FromFile(newFile.UploadedFile)); } } if (!_isFsProvider) { // MediaFile.MediaStorageId has been updated, we need to save again. num = scope.Commit(); } } catch { throw; } finally { newFiles.Clear(); // Breathe ctx.DetachEntities <MediaFile>(deep: true); } foreach (var uploadedFolder in _mediaFileSystem.ListFolders(folder.Path)) { var mediaFolder = new MediaFolder { Name = uploadedFolder.Name, ParentId = mediaFolderId }; // Add folder and save ASAP, we need the folder id folderSet.Add(mediaFolder); ctx.SaveChanges(); ProcessFolder(uploadedFolder, mediaFolder.Id); } } } }
public Task SaveAsync(MediaFile media, MediaStorageItem item) { return(Task.CompletedTask); }
public void Save(MediaFile media, MediaStorageItem item) { }