private void GenerateThumbnail(IFileDao <T> fileDao, FileData <T> fileData) { File <T> file = null; try { file = fileDao.GetFile(fileData.FileId); if (file == null) { logger.ErrorFormat("GenerateThumbnail: FileId: {0}. File not found.", fileData.FileId); return; } if (file.ThumbnailStatus != Thumbnail.Waiting) { logger.InfoFormat("GenerateThumbnail: FileId: {0}. Thumbnail already processed.", fileData.FileId); return; } var ext = FileUtility.GetFileExtension(file.Title); if (!config.FormatsArray.Contains(ext) || file.Encrypted || file.RootFolderType == FolderType.TRASH || file.ContentLength > config.AvailableFileSize) { file.ThumbnailStatus = Thumbnail.NotRequired; fileDao.SaveThumbnail(file, null); return; } if (IsImage(file)) { CropImage(fileDao, file); } else { MakeThumbnail(fileDao, file); } } catch (Exception exception) { logger.Error(string.Format("GenerateThumbnail: FileId: {0}.", fileData.FileId), exception); if (file != null) { file.ThumbnailStatus = Thumbnail.Error; fileDao.SaveThumbnail(file, null); } } }
private void SaveThumbnail(IFileDao fileDao, File file, string thumbnailUrl) { logger.DebugFormat("SaveThumbnail: FileId: {0}. ThumbnailUrl {1}.", file.ID, thumbnailUrl); var req = (HttpWebRequest)WebRequest.Create(thumbnailUrl); //HACK: http://ubuntuforums.org/showthread.php?t=1841740 if (WorkContext.IsMono && ServicePointManager.ServerCertificateValidationCallback == null) { ServicePointManager.ServerCertificateValidationCallback += (s, c, n, p) => true; } using (var stream = new ResponseStream(req.GetResponse())) { if (config.ThumbnailAspect == 3) { fileDao.SaveThumbnail(file, stream); } else { Crop(fileDao, file, stream); } } logger.DebugFormat("SaveThumbnail: FileId: {0}. Successfully saved.", file.ID); }
private void Crop(IFileDao fileDao, File file, Stream stream) { using (var sourceBitmap = new Bitmap(stream)) { using (var targetBitmap = GetImageThumbnail(sourceBitmap)) { using (var targetStream = new MemoryStream()) { targetBitmap.Save(targetStream, System.Drawing.Imaging.ImageFormat.Png); fileDao.SaveThumbnail(file, targetStream); } } } }
private static void SaveFile(IFileDao fileDao, object folder, string filePath, IDataStore storeTemp) { try { if (FileUtility.GetFileExtension(filePath) == "." + ThumbnailExtension && storeTemp.IsFile("", Regex.Replace(filePath, "\\." + ThumbnailExtension + "$", ""))) { return; } var fileName = Path.GetFileName(filePath); var file = new File { Title = fileName, FolderID = folder, Comment = FilesCommonResource.CommentCreate, }; using (var stream = storeTemp.GetReadStream("", filePath)) { file.ContentLength = stream.CanSeek ? stream.Length : storeTemp.GetFileSize("", filePath); file = fileDao.SaveFile(file, stream); } var pathThumb = filePath + "." + ThumbnailExtension; if (storeTemp.IsFile("", pathThumb)) { using (var streamThumb = storeTemp.GetReadStream("", pathThumb)) { fileDao.SaveThumbnail(file, streamThumb); } file.ThumbnailStatus = Thumbnail.Created; } FileMarker.MarkAsNew(file); } catch (Exception ex) { Logger.Error(ex); } }
public File <TTo> PerformCrossDaoFileCopy <TFrom, TTo>( TFrom fromFileId, IFileDao <TFrom> fromFileDao, Func <TFrom, TFrom> fromConverter, TTo toFolderId, IFileDao <TTo> toFileDao, Func <TTo, TTo> toConverter, bool deleteSourceFile) { //Get File from first dao var fromFile = fromFileDao.GetFile(fromConverter(fromFileId)); if (fromFile.ContentLength > SetupInfo.AvailableFileSize) { throw new Exception(string.Format(deleteSourceFile ? FilesCommonResource.ErrorMassage_FileSizeMove : FilesCommonResource.ErrorMassage_FileSizeCopy, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize))); } var securityDao = ServiceProvider.GetService <ISecurityDao <TFrom> >(); var tagDao = ServiceProvider.GetService <ITagDao <TFrom> >(); var fromFileShareRecords = securityDao.GetPureShareRecords(fromFile).Where(x => x.EntryType == FileEntryType.File); var fromFileNewTags = tagDao.GetNewTags(Guid.Empty, fromFile).ToList(); var fromFileLockTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Locked).FirstOrDefault(); var fromFileFavoriteTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Favorite); var fromFileTemplateTag = tagDao.GetTags(fromFile.ID, FileEntryType.File, TagType.Template); var toFile = ServiceProvider.GetService <File <TTo> >(); toFile.Title = fromFile.Title; toFile.Encrypted = fromFile.Encrypted; toFile.FolderID = toConverter(toFolderId); fromFile.ID = fromConverter(fromFile.ID); var mustConvert = !string.IsNullOrEmpty(fromFile.ConvertedType); using (var fromFileStream = mustConvert ? FileConverter.Exec(fromFile) : fromFileDao.GetFileStream(fromFile)) { toFile.ContentLength = fromFileStream.CanSeek ? fromFileStream.Length : fromFile.ContentLength; toFile = toFileDao.SaveFile(toFile, fromFileStream); } if (fromFile.ThumbnailStatus == Thumbnail.Created) { using (var thumbnail = fromFileDao.GetThumbnail(fromFile)) { toFileDao.SaveThumbnail(toFile, thumbnail); } toFile.ThumbnailStatus = Thumbnail.Created; } if (deleteSourceFile) { if (fromFileShareRecords.Any()) { fromFileShareRecords.ToList().ForEach(x => { x.EntryId = toFile.ID; securityDao.SetShare(x); }); } var fromFileTags = fromFileNewTags; if (fromFileLockTag != null) { fromFileTags.Add(fromFileLockTag); } if (fromFileFavoriteTag != null) { fromFileTags.AddRange(fromFileFavoriteTag); } if (fromFileTemplateTag != null) { fromFileTags.AddRange(fromFileTemplateTag); } if (fromFileTags.Any()) { fromFileTags.ForEach(x => x.EntryId = toFile.ID); tagDao.SaveTags(fromFileTags); } //Delete source file if needed fromFileDao.DeleteFile(fromConverter(fromFileId)); } return(toFile); }