Exemplo n.º 1
0
 public void Deconstruct(out IOptionsMonitor <ILog> optionsMonitor,
                         out TenantManager tenantManager,
                         out UserManager userManager,
                         out SecurityContext securityContext,
                         out IDaoFactory daoFactory,
                         out FileSecurity fileSecurity,
                         out PathProvider pathProvider,
                         out SetupInfo setupInfo,
                         out FileUtility fileUtility,
                         out DocumentServiceHelper documentServiceHelper,
                         out DocumentServiceConnector documentServiceConnector,
                         out EntryManager entryManager,
                         out FileConverter fileConverter)
 {
     optionsMonitor           = Options;
     tenantManager            = TenantManager;
     userManager              = UserManager;
     securityContext          = SecurityContext;
     daoFactory               = DaoFactory;
     fileSecurity             = FileSecurity;
     pathProvider             = PathProvider;
     setupInfo                = SetupInfo;
     fileUtility              = FileUtility;
     documentServiceHelper    = DocumentServiceHelper;
     documentServiceConnector = DocumentServiceConnector;
     entryManager             = EntryManager;
     fileConverter            = FileConverter;
 }
Exemplo n.º 2
0
 public FileConverter(
     FileUtility fileUtility,
     FilesLinkUtility filesLinkUtility,
     IDaoFactory daoFactory,
     SetupInfo setupInfo,
     PathProvider pathProvider,
     FileSecurity fileSecurity,
     FileMarker fileMarker,
     TenantManager tenantManager,
     AuthContext authContext,
     EntryManager entryManager,
     IOptionsMonitor <ILog> options,
     FilesSettingsHelper filesSettingsHelper,
     GlobalFolderHelper globalFolderHelper,
     FilesMessageService filesMessageService,
     FileShareLink fileShareLink,
     DocumentServiceHelper documentServiceHelper,
     DocumentServiceConnector documentServiceConnector,
     IServiceProvider serviceProvider,
     IHttpContextAccessor httpContextAccesor)
     : this(fileUtility, filesLinkUtility, daoFactory, setupInfo, pathProvider, fileSecurity,
            fileMarker, tenantManager, authContext, entryManager, options, filesSettingsHelper,
            globalFolderHelper, filesMessageService, fileShareLink, documentServiceHelper, documentServiceConnector,
            serviceProvider)
 {
     HttpContextAccesor = httpContextAccesor;
 }
Exemplo n.º 3
0
 public FileConverterQueueScope(IOptionsMonitor <ILog> options,
                                TenantManager tenantManager,
                                UserManager userManager,
                                SecurityContext securityContext,
                                IDaoFactory daoFactory,
                                FileSecurity fileSecurity,
                                PathProvider pathProvider,
                                SetupInfo setupInfo,
                                FileUtility fileUtility,
                                DocumentServiceHelper documentServiceHelper,
                                DocumentServiceConnector documentServiceConnector,
                                EntryManager entryManager,
                                FileConverter fileConverter)
 {
     Options                  = options;
     TenantManager            = tenantManager;
     UserManager              = userManager;
     SecurityContext          = securityContext;
     DaoFactory               = daoFactory;
     FileSecurity             = fileSecurity;
     PathProvider             = pathProvider;
     SetupInfo                = setupInfo;
     FileUtility              = fileUtility;
     DocumentServiceHelper    = documentServiceHelper;
     DocumentServiceConnector = documentServiceConnector;
     EntryManager             = entryManager;
     FileConverter            = fileConverter;
 }
Exemplo n.º 4
0
        private string FileJsonSerializer(EntryManager EntryManager, File <T> file, string folderTitle)
        {
            if (file == null)
            {
                return(string.Empty);
            }

            EntryManager.SetFileStatus(file);

            var options = new JsonSerializerOptions()
            {
                IgnoreNullValues         = true,
                IgnoreReadOnlyProperties = true,
                WriteIndented            = false
            };

            return(JsonSerializer.Serialize(
                       new FileJsonSerializerData <T>()
            {
                Id = file.ID,
                Title = file.Title,
                Version = file.Version,
                FolderID = file.FolderID,
                FolderTitle = folderTitle ?? "",
                FileJson = JsonSerializer.Serialize(file, options)
            }, options));
        }
Exemplo n.º 5
0
 private static String FileJsonSerializer(File file, bool removeoriginal, string folderTitle)
 {
     EntryManager.SetFileStatus(file);
     return(string.Format("{{ \"file\": {{ \"id\": \"{0}\", \"title\": \"{1}\", \"version\": \"{2}\", \"fileXml\": \"{3}\"}}, \"removeOriginal\": {4}, \"folderId\": \"{5}\", \"folderTitle\": \"{6}\" }}",
                          file.ID, file.Title, file.Version,
                          File.Serialize(file).Replace('"', '\''), removeoriginal.ToString().ToLower(),
                          file.FolderID, folderTitle));
 }
Exemplo n.º 6
0
 private static bool CanEdit(File file)
 {
     return(file != null &&
            Global.GetFilesSecurity().CanEdit(file) &&
            !CoreContext.UserManager.GetUsers(SecurityContext.CurrentAccount.ID).IsVisitor() &&
            !EntryManager.FileLockedForMe(file.ID) &&
            !FileTracker.IsEditing(file.ID) &&
            file.RootFolderType != FolderType.TRASH);
 }
Exemplo n.º 7
0
 private bool CanEdit <T>(File <T> file)
 {
     return(file != null &&
            FileSecurity.CanEdit(file) &&
            !UserManager.GetUsers(AuthContext.CurrentAccount.ID).IsVisitor(UserManager) &&
            !EntryManager.FileLockedForMe(file.ID) &&
            !FileTracker.IsEditing(file.ID) &&
            file.RootFolderType != FolderType.TRASH &&
            !file.Encrypted);
 }
Exemplo n.º 8
0
 public FileConverter(
     FileUtility fileUtility,
     FilesLinkUtility filesLinkUtility,
     IDaoFactory daoFactory,
     SetupInfo setupInfo,
     PathProvider pathProvider,
     FileSecurity fileSecurity,
     FileMarker fileMarker,
     TenantManager tenantManager,
     AuthContext authContext,
     EntryManager entryManager,
     FilesSettingsHelper filesSettingsHelper,
     GlobalFolderHelper globalFolderHelper,
     FilesMessageService filesMessageService,
     FileShareLink fileShareLink,
     DocumentServiceHelper documentServiceHelper,
     DocumentServiceConnector documentServiceConnector,
     FileTrackerHelper fileTracker,
     BaseCommonLinkUtility baseCommonLinkUtility,
     EntryStatusManager entryStatusManager,
     IServiceProvider serviceProvider)
 {
     FileUtility              = fileUtility;
     FilesLinkUtility         = filesLinkUtility;
     DaoFactory               = daoFactory;
     SetupInfo                = setupInfo;
     PathProvider             = pathProvider;
     FileSecurity             = fileSecurity;
     FileMarker               = fileMarker;
     TenantManager            = tenantManager;
     AuthContext              = authContext;
     EntryManager             = entryManager;
     FilesSettingsHelper      = filesSettingsHelper;
     GlobalFolderHelper       = globalFolderHelper;
     FilesMessageService      = filesMessageService;
     FileShareLink            = fileShareLink;
     DocumentServiceHelper    = documentServiceHelper;
     DocumentServiceConnector = documentServiceConnector;
     FileTracker              = fileTracker;
     BaseCommonLinkUtility    = baseCommonLinkUtility;
     EntryStatusManager       = entryStatusManager;
     ServiceProvider          = serviceProvider;
 }
Exemplo n.º 9
0
 public FileUploader(
     FilesSettingsHelper filesSettingsHelper,
     FileUtility fileUtility,
     UserManager userManager,
     TenantManager tenantManager,
     AuthContext authContext,
     SetupInfo setupInfo,
     TenantExtra tenantExtra,
     TenantStatisticsProvider tenantStatisticsProvider,
     FileMarker fileMarker,
     FileConverter fileConverter,
     IDaoFactory daoFactory,
     Global global,
     FilesLinkUtility filesLinkUtility,
     FilesMessageService filesMessageService,
     FileSecurity fileSecurity,
     EntryManager entryManager,
     IServiceProvider serviceProvider,
     ChunkedUploadSessionHolder chunkedUploadSessionHolder,
     FileTrackerHelper fileTracker)
 {
     FilesSettingsHelper      = filesSettingsHelper;
     FileUtility              = fileUtility;
     UserManager              = userManager;
     TenantManager            = tenantManager;
     AuthContext              = authContext;
     SetupInfo                = setupInfo;
     TenantExtra              = tenantExtra;
     TenantStatisticsProvider = tenantStatisticsProvider;
     FileMarker               = fileMarker;
     FileConverter            = fileConverter;
     DaoFactory               = daoFactory;
     Global                     = global;
     FilesLinkUtility           = filesLinkUtility;
     FilesMessageService        = filesMessageService;
     FileSecurity               = fileSecurity;
     EntryManager               = entryManager;
     ServiceProvider            = serviceProvider;
     ChunkedUploadSessionHolder = chunkedUploadSessionHolder;
     FileTracker                = fileTracker;
 }
Exemplo n.º 10
0
 public FileConverter(
     FileUtility fileUtility,
     FilesLinkUtility filesLinkUtility,
     IDaoFactory daoFactory,
     SetupInfo setupInfo,
     PathProvider pathProvider,
     FileSecurity fileSecurity,
     FileMarker fileMarker,
     TenantManager tenantManager,
     AuthContext authContext,
     EntryManager entryManager,
     IOptionsMonitor <ILog> options,
     FilesSettingsHelper filesSettingsHelper,
     GlobalFolderHelper globalFolderHelper,
     FilesMessageService filesMessageService,
     FileShareLink fileShareLink,
     DocumentServiceHelper documentServiceHelper,
     DocumentServiceConnector documentServiceConnector,
     IServiceProvider serviceProvider)
 {
     FileUtility              = fileUtility;
     FilesLinkUtility         = filesLinkUtility;
     DaoFactory               = daoFactory;
     SetupInfo                = setupInfo;
     PathProvider             = pathProvider;
     FileSecurity             = fileSecurity;
     FileMarker               = fileMarker;
     TenantManager            = tenantManager;
     AuthContext              = authContext;
     EntryManager             = entryManager;
     FilesSettingsHelper      = filesSettingsHelper;
     GlobalFolderHelper       = globalFolderHelper;
     FilesMessageService      = filesMessageService;
     FileShareLink            = fileShareLink;
     DocumentServiceHelper    = documentServiceHelper;
     DocumentServiceConnector = documentServiceConnector;
     ServiceProvider          = serviceProvider;
     Logger = options.CurrentValue;
     cache  = AscCache.Memory;
 }
Exemplo n.º 11
0
        private string FileJsonSerializer(File file, string folderTitle)
        {
            if (file == null)
            {
                return(string.Empty);
            }

            EntryManager.SetFileStatus(file);
            return
                (string.Format("{{ \"id\": \"{0}\"," +
                               " \"title\": \"{1}\"," +
                               " \"version\": \"{2}\"," +
                               " \"folderId\": \"{3}\"," +
                               " \"folderTitle\": \"{4}\"," +
                               " \"fileXml\": \"{5}\" }}",
                               file.ID,
                               file.Title,
                               file.Version,
                               file.FolderID,
                               folderTitle ?? "",
                               File.Serialize(file).Replace('"', '\'')));
        }
Exemplo n.º 12
0
        private File SaveConvertedFile(File file, string convertedFileUrl)
        {
            var  fileSecurity = FileSecurity;
            var  fileDao      = DaoFactory.FileDao;
            var  folderDao    = DaoFactory.FolderDao;
            File newFile      = null;
            var  newFileTitle = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetInternalExtension(file.Title));

            if (!FilesSettingsHelper.StoreOriginalFiles && fileSecurity.CanEdit(file))
            {
                newFile = (File)file.Clone();
                newFile.Version++;
            }
            else
            {
                var folderId = GlobalFolderHelper.FolderMy;

                var parent = folderDao.GetFolder(file.FolderID);
                if (parent != null &&
                    fileSecurity.CanCreate(parent))
                {
                    folderId = parent.ID;
                }

                if (Equals(folderId, 0))
                {
                    throw new SecurityException(FilesCommonResource.ErrorMassage_FolderNotFound);
                }

                if (FilesSettingsHelper.UpdateIfExist && (parent != null && folderId != parent.ID || !file.ProviderEntry))
                {
                    newFile = fileDao.GetFile(folderId, newFileTitle);
                    if (newFile != null && fileSecurity.CanEdit(newFile) && !EntryManager.FileLockedForMe(newFile.ID) && !FileTracker.IsEditing(newFile.ID))
                    {
                        newFile.Version++;
                    }
                    else
                    {
                        newFile = null;
                    }
                }

                if (newFile == null)
                {
                    newFile          = ServiceProvider.GetService <File>();
                    newFile.FolderID = folderId;
                }
            }

            newFile.Title         = newFileTitle;
            newFile.ConvertedType = null;
            newFile.Comment       = string.Format(FilesCommonResource.CommentConvert, file.Title);

            var req = (HttpWebRequest)WebRequest.Create(convertedFileUrl);

            if (WorkContext.IsMono && ServicePointManager.ServerCertificateValidationCallback == null)
            {
                ServicePointManager.ServerCertificateValidationCallback += (s, c, n, p) => true; //HACK: http://ubuntuforums.org/showthread.php?t=1841740
            }

            try
            {
                using (var convertedFileStream = new ResponseStream(req.GetResponse()))
                {
                    newFile.ContentLength = convertedFileStream.Length;
                    newFile = fileDao.SaveFile(newFile, convertedFileStream);
                }
            }
            catch (WebException e)
            {
                using var response = e.Response;
                var httpResponse = (HttpWebResponse)response;
                var errorString  = string.Format("WebException: {0}", httpResponse.StatusCode);

                if (httpResponse.StatusCode != HttpStatusCode.NotFound)
                {
                    using var responseStream = response.GetResponseStream();
                    if (responseStream != null)
                    {
                        using var readStream = new StreamReader(responseStream);
                        var text = readStream.ReadToEnd();
                        errorString += string.Format(" Error message: {0}", text);
                    }
                }

                throw new Exception(errorString);
            }

            FilesMessageService.Send(newFile, MessageInitiator.DocsService, MessageAction.FileConverted, newFile.Title);
            FileMarker.MarkAsNew(newFile);

            var tagDao = DaoFactory.TagDao;
            var tags   = tagDao.GetTags(file.ID, FileEntryType.File, TagType.System).ToList();

            if (tags.Any())
            {
                tags.ForEach(r => r.EntryId = newFile.ID);
                tagDao.SaveTags(tags);
            }

            return(newFile);
        }
Exemplo n.º 13
0
        private static void CheckConvertFilesStatus(Object obj)
        {
            lock (LockerTimer)
            {
                _timer.Change(Timeout.Infinite, Timeout.Infinite);
            }
            try
            {
                List <File> filesIsConverting;

                lock (LockerStatus)
                {
                    ConversionFileStatus.Where(x => ((!String.IsNullOrEmpty(x.Value.Processed) &&
                                                      DateTime.Now.Subtract(x.Value.StopDateTime) > TimeSpan.FromMinutes(30))))
                    .ToList().ForEach(x => ConversionFileStatus.Remove(x));

                    filesIsConverting = ConversionFileStatus.Where(x => String.IsNullOrEmpty(x.Value.Processed)).Select(x => x.Key).ToList();
                }

                if (filesIsConverting.Count == 0)
                {
                    lock (LockerTimer)
                    {
                        _timer.Dispose();
                        _timer = null;
                    }
                    return;
                }

                foreach (var file in filesIsConverting)
                {
                    var    fileUri = file.ID.ToString();
                    string convetedFileUrl;
                    int    operationResultProgress;
                    object folderId;
                    var    currentFolder = false;

                    try
                    {
                        int      tenantId;
                        IAccount account;

                        lock (LockerStatus)
                        {
                            var operationResult = ConversionFileStatus[file];
                            if (operationResult == null)
                            {
                                continue;
                            }
                            tenantId = operationResult.TenantId;
                            account  = operationResult.Account;
                        }

                        CoreContext.TenantManager.SetCurrentTenant(tenantId);
                        SecurityContext.AuthenticateMe(account);

                        var user    = CoreContext.UserManager.GetUsers(account.ID);
                        var culture = string.IsNullOrEmpty(user.CultureName)
                                          ? CoreContext.TenantManager.GetCurrentTenant().GetCulture()
                                          : CultureInfo.GetCultureInfo(user.CultureName);
                        Thread.CurrentThread.CurrentCulture   = culture;
                        Thread.CurrentThread.CurrentUICulture = culture;

                        var fileSecurity = Global.GetFilesSecurity();
                        if (!fileSecurity.CanRead(file) &&
                            file.RootFolderType != FolderType.BUNCH)    //No rights in CRM after upload before attach
                        {
                            throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
                        }
                        if (file.ContentLength > SetupInfo.AvailableFileSize)
                        {
                            throw new Exception(string.Format(FilesCommonResource.ErrorMassage_FileSizeConvert, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)));
                        }

                        folderId = Global.FolderMy;
                        using (var folderDao = Global.DaoFactory.GetFolderDao())
                        {
                            var parent = folderDao.GetFolder(file.FolderID);
                            if (parent != null &&
                                fileSecurity.CanCreate(parent))
                            {
                                folderId      = parent.ID;
                                currentFolder = true;
                            }
                        }
                        if (Equals(folderId, 0))
                        {
                            throw new SecurityException(FilesCommonResource.ErrorMassage_FolderNotFound);
                        }

                        fileUri = PathProvider.GetFileStreamUrl(file);

                        var toExtension   = FileUtility.GetInternalExtension(file.Title);
                        var fileExtension = file.ConvertedType ?? FileUtility.GetFileExtension(file.Title);
                        var docKey        = DocumentServiceHelper.GetDocKey(file.ID, file.Version, file.ModifiedOn);

                        operationResultProgress = DocumentServiceConnector.GetConvertedUri(fileUri, fileExtension, toExtension, docKey, true, out convetedFileUrl);
                    }
                    catch (Exception exception)
                    {
                        lock (LockerStatus)
                        {
                            var operationResult = ConversionFileStatus[file];
                            if (operationResult != null)
                            {
                                if (operationResult.Delete)
                                {
                                    ConversionFileStatus.Remove(file);
                                }
                                else
                                {
                                    operationResult.Result       = FileJsonSerializer(file);
                                    operationResult.Processed    = "1";
                                    operationResult.StopDateTime = DateTime.Now;
                                    operationResult.Error        = exception.Message;
                                }
                            }
                        }

                        var strExc = exception.Message + " in file " + fileUri;
                        Global.Logger.Error(strExc, exception);

                        continue;
                    }

                    if (operationResultProgress < 100)
                    {
                        lock (LockerStatus)
                        {
                            var operationResult = ConversionFileStatus[file];
                            if (operationResult != null)
                            {
                                operationResult.Progress = operationResultProgress;
                            }
                        }
                        continue;
                    }

                    using (var fileDao = Global.DaoFactory.GetFileDao())
                        using (var folderDao = Global.DaoFactory.GetFolderDao())
                        {
                            var newFileTitle = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetInternalExtension(file.Title));

                            File newFile = null;
                            if (FilesSettings.UpdateIfExist && (!currentFolder || !file.ProviderEntry))
                            {
                                newFile = fileDao.GetFile(folderId, newFileTitle);
                                if (newFile != null &&
                                    Global.GetFilesSecurity().CanEdit(newFile) &&
                                    !EntryManager.FileLockedForMe(newFile.ID) &&
                                    !FileTracker.IsEditing(newFile.ID))
                                {
                                    newFile.Version++;
                                }
                                else
                                {
                                    newFile = null;
                                }
                            }

                            if (newFile == null)
                            {
                                newFile = new File {
                                    FolderID = folderId
                                }
                            }
                            ;

                            newFile.Title         = newFileTitle;
                            newFile.ConvertedType = FileUtility.GetInternalExtension(file.Title);

                            var operationResultError = string.Empty;
                            try
                            {
                                var req = (HttpWebRequest)WebRequest.Create(convetedFileUrl);

                                using (var convertedFileStream = new ResponseStream(req.GetResponse()))
                                {
                                    newFile.ContentLength = convertedFileStream.Length;
                                    newFile.Comment       = string.Empty;
                                    newFile = fileDao.SaveFile(newFile, convertedFileStream);
                                }

                                FileMarker.MarkAsNew(newFile);

                                using (var tagDao = Global.DaoFactory.GetTagDao())
                                {
                                    var tags = tagDao.GetTags(file.ID, FileEntryType.File, TagType.System).ToList();
                                    if (tags.Any())
                                    {
                                        tags.ForEach(r => r.EntryId = newFile.ID);
                                        tagDao.SaveTags(tags.ToArray());
                                    }
                                }

                                operationResultProgress = 100;
                            }
                            catch (WebException e)
                            {
                                using (var response = e.Response)
                                {
                                    var httpResponse = (HttpWebResponse)response;
                                    var errorString  = String.Format("Error code: {0}", httpResponse.StatusCode);

                                    if (httpResponse.StatusCode != HttpStatusCode.NotFound)
                                    {
                                        using (var data = response.GetResponseStream())
                                        {
                                            var text = new StreamReader(data).ReadToEnd();
                                            errorString += String.Format(" Error message: {0}", text);
                                        }
                                    }

                                    operationResultError = errorString;

                                    Global.Logger.Error(errorString + "  ConvertUrl : " + convetedFileUrl + "    fromUrl : " + fileUri, e);
                                    throw new Exception(errorString);
                                }
                            }
                            finally
                            {
                                var fileSecurity   = Global.GetFilesSecurity();
                                var removeOriginal = !FilesSettings.StoreOriginalFiles &&
                                                     fileSecurity.CanDelete(file) &&
                                                     currentFolder &&
                                                     !EntryManager.FileLockedForMe(file.ID);

                                var folderTitle = folderDao.GetFolder(newFile.FolderID).Title;

                                lock (LockerStatus)
                                {
                                    var operationResult = ConversionFileStatus[file];

                                    if (operationResult.Delete)
                                    {
                                        ConversionFileStatus.Remove(file);
                                    }
                                    else
                                    {
                                        operationResult.Result       = FileJsonSerializer(newFile, removeOriginal, folderTitle);
                                        operationResult.Processed    = "1";
                                        operationResult.StopDateTime = DateTime.Now;
                                        operationResult.Progress     = operationResultProgress;
                                        if (!string.IsNullOrEmpty(operationResultError))
                                        {
                                            operationResult.Error = operationResultError;
                                        }
                                    }
                                }

                                if (removeOriginal)
                                {
                                    FileMarker.RemoveMarkAsNewForAll(file);
                                    fileDao.DeleteFile(file.ID);
                                }
                            }
                        }
                }

                lock (LockerTimer)
                {
                    _timer.Change(0, TimerConvertPeriod);
                }
            }
            catch (Exception exception)
            {
                Global.Logger.Error(exception.Message, exception);
                lock (LockerTimer)
                {
                    _timer.Dispose();
                    _timer = null;
                }
            }
        }
Exemplo n.º 14
0
        private static void CheckConvertFilesStatus(object _)
        {
            if (Monitor.TryEnter(singleThread))
            {
                try
                {
                    List <File> filesIsConverting;
                    lock (locker)
                    {
                        timer.Change(Timeout.Infinite, Timeout.Infinite);

                        conversionQueue.Where(x => !string.IsNullOrEmpty(x.Value.Processed) &&
                                              (x.Value.Progress == 100 && DateTime.Now - x.Value.StopDateTime > TimeSpan.FromMinutes(1) ||
                                               DateTime.Now - x.Value.StopDateTime > TimeSpan.FromMinutes(30)))
                        .ToList()
                        .ForEach(x =>
                        {
                            conversionQueue.Remove(x);
                            cache.Remove(GetKey(x.Key));
                        });

                        Global.Logger.DebugFormat("Run CheckConvertFilesStatus: count {0}", conversionQueue.Count);

                        if (conversionQueue.Count == 0)
                        {
                            return;
                        }

                        filesIsConverting = conversionQueue
                                            .Where(x => String.IsNullOrEmpty(x.Value.Processed))
                                            .Select(x => x.Key)
                                            .ToList();
                    }

                    foreach (var file in filesIsConverting)
                    {
                        var    fileUri = file.ID.ToString();
                        string convertedFileUrl;
                        int    operationResultProgress;
                        object folderId;
                        var    currentFolder = false;

                        try
                        {
                            int      tenantId;
                            IAccount account;

                            lock (locker)
                            {
                                if (!conversionQueue.Keys.Contains(file))
                                {
                                    continue;
                                }

                                var operationResult = conversionQueue[file];
                                if (!string.IsNullOrEmpty(operationResult.Processed))
                                {
                                    continue;
                                }

                                operationResult.Processed = "1";
                                tenantId = operationResult.TenantId;
                                account  = operationResult.Account;

                                cache.Insert(GetKey(file), operationResult, TimeSpan.FromMinutes(30));
                            }

                            CoreContext.TenantManager.SetCurrentTenant(tenantId);
                            SecurityContext.AuthenticateMe(account);

                            var user    = CoreContext.UserManager.GetUsers(account.ID);
                            var culture = string.IsNullOrEmpty(user.CultureName) ? CoreContext.TenantManager.GetCurrentTenant().GetCulture() : CultureInfo.GetCultureInfo(user.CultureName);
                            Thread.CurrentThread.CurrentCulture   = culture;
                            Thread.CurrentThread.CurrentUICulture = culture;

                            var fileSecurity = Global.GetFilesSecurity();
                            if (!fileSecurity.CanRead(file) && file.RootFolderType != FolderType.BUNCH)
                            {
                                //No rights in CRM after upload before attach
                                throw new SecurityException(FilesCommonResource.ErrorMassage_SecurityException_ReadFile);
                            }
                            if (file.ContentLength > SetupInfo.AvailableFileSize)
                            {
                                throw new Exception(string.Format(FilesCommonResource.ErrorMassage_FileSizeConvert, FileSizeComment.FilesSizeToString(SetupInfo.AvailableFileSize)));
                            }

                            folderId = Global.FolderMy;
                            using (var folderDao = Global.DaoFactory.GetFolderDao())
                            {
                                var parent = folderDao.GetFolder(file.FolderID);
                                if (parent != null &&
                                    fileSecurity.CanCreate(parent))
                                {
                                    folderId      = parent.ID;
                                    currentFolder = true;
                                }
                            }
                            if (Equals(folderId, 0))
                            {
                                throw new SecurityException(FilesCommonResource.ErrorMassage_FolderNotFound);
                            }

                            fileUri = PathProvider.GetFileStreamUrl(file);

                            var toExtension   = FileUtility.GetInternalExtension(file.Title);
                            var fileExtension = file.ConvertedExtension;
                            var docKey        = DocumentServiceHelper.GetDocKey(file.ID, file.Version, file.ModifiedOn);

                            operationResultProgress = DocumentServiceConnector.GetConvertedUri(fileUri, fileExtension, toExtension, docKey, true, out convertedFileUrl);
                            operationResultProgress = Math.Min(operationResultProgress, 100);
                        }
                        catch (Exception exception)
                        {
                            Global.Logger.ErrorFormat("Error convert {0} with url {1}: {2}", file.ID, fileUri, exception);
                            lock (locker)
                            {
                                if (conversionQueue.Keys.Contains(file))
                                {
                                    var operationResult = conversionQueue[file];
                                    if (operationResult.Delete)
                                    {
                                        conversionQueue.Remove(file);
                                        cache.Remove(GetKey(file));
                                    }
                                    else
                                    {
                                        operationResult.Result       = FileJsonSerializer(file);
                                        operationResult.Progress     = 100;
                                        operationResult.StopDateTime = DateTime.Now;
                                        operationResult.Error        = exception.Message;
                                        cache.Insert(GetKey(file), operationResult, TimeSpan.FromMinutes(30));
                                    }
                                }
                            }
                            continue;
                        }

                        if (operationResultProgress < 100)
                        {
                            lock (locker)
                            {
                                if (conversionQueue.Keys.Contains(file))
                                {
                                    var operationResult = conversionQueue[file];
                                    operationResult.Processed = "";
                                    operationResult.Progress  = operationResultProgress;
                                    cache.Insert(GetKey(file), operationResult, TimeSpan.FromMinutes(30));
                                }
                            }
                            continue;
                        }

                        using (var fileDao = Global.DaoFactory.GetFileDao())
                            using (var folderDao = Global.DaoFactory.GetFolderDao())
                            {
                                var newFileTitle = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetInternalExtension(file.Title));

                                File newFile = null;
                                if (FilesSettings.UpdateIfExist && (!currentFolder || !file.ProviderEntry))
                                {
                                    newFile = fileDao.GetFile(folderId, newFileTitle);
                                    if (newFile != null && Global.GetFilesSecurity().CanEdit(newFile) && !EntryManager.FileLockedForMe(newFile.ID) && !FileTracker.IsEditing(newFile.ID))
                                    {
                                        newFile.Version++;
                                    }
                                    else
                                    {
                                        newFile = null;
                                    }
                                }

                                if (newFile == null)
                                {
                                    newFile = new File {
                                        FolderID = folderId
                                    };
                                }
                                newFile.Title         = newFileTitle;
                                newFile.ConvertedType = null;
                                newFile.Comment       = string.Format(FilesCommonResource.CommentConvert, file.Title);

                                var operationResultError = string.Empty;
                                try
                                {
                                    var req = (HttpWebRequest)WebRequest.Create(convertedFileUrl);

                                    if (WorkContext.IsMono && ServicePointManager.ServerCertificateValidationCallback == null)
                                    {
                                        ServicePointManager.ServerCertificateValidationCallback += (s, c, n, p) => true; //HACK: http://ubuntuforums.org/showthread.php?t=1841740
                                    }

                                    using (var convertedFileStream = new ResponseStream(req.GetResponse()))
                                    {
                                        newFile.ContentLength = convertedFileStream.Length;
                                        newFile = fileDao.SaveFile(newFile, convertedFileStream);
                                    }

                                    FilesMessageService.Send(newFile, MessageInitiator.DocsService, MessageAction.FileConverted, newFile.Title);
                                    FileMarker.MarkAsNew(newFile);

                                    using (var tagDao = Global.DaoFactory.GetTagDao())
                                    {
                                        var tags = tagDao.GetTags(file.ID, FileEntryType.File, TagType.System).ToList();
                                        if (tags.Any())
                                        {
                                            tags.ForEach(r => r.EntryId = newFile.ID);
                                            tagDao.SaveTags(tags.ToArray());
                                        }
                                    }

                                    operationResultProgress = 100;
                                }
                                catch (WebException e)
                                {
                                    using (var response = e.Response)
                                    {
                                        var httpResponse = (HttpWebResponse)response;
                                        var errorString  = String.Format("Error code: {0}", httpResponse.StatusCode);

                                        if (httpResponse.StatusCode != HttpStatusCode.NotFound)
                                        {
                                            using (var data = response.GetResponseStream())
                                            {
                                                var text = new StreamReader(data).ReadToEnd();
                                                errorString += String.Format(" Error message: {0}", text);
                                            }
                                        }

                                        operationResultProgress = 100;
                                        operationResultError    = errorString;

                                        Global.Logger.ErrorFormat("{0} ConvertUrl: {1} fromUrl: {2}: {3}", errorString, convertedFileUrl, fileUri, e);
                                        throw new Exception(errorString);
                                    }
                                }
                                finally
                                {
                                    var fileSecurity   = Global.GetFilesSecurity();
                                    var removeOriginal = !FilesSettings.StoreOriginalFiles && fileSecurity.CanDelete(file) && currentFolder && !EntryManager.FileLockedForMe(file.ID);
                                    var folderTitle    = folderDao.GetFolder(newFile.FolderID).Title;

                                    lock (locker)
                                    {
                                        if (conversionQueue.Keys.Contains(file))
                                        {
                                            var operationResult = conversionQueue[file];
                                            if (operationResult.Delete)
                                            {
                                                conversionQueue.Remove(file);
                                                cache.Remove(GetKey(file));
                                            }
                                            else
                                            {
                                                operationResult.Result       = FileJsonSerializer(newFile, removeOriginal, folderTitle);
                                                operationResult.StopDateTime = DateTime.Now;
                                                operationResult.Processed    = "1";
                                                operationResult.Progress     = operationResultProgress;
                                                if (!string.IsNullOrEmpty(operationResultError))
                                                {
                                                    operationResult.Error = operationResultError;
                                                }
                                                cache.Insert(GetKey(file), operationResult, TimeSpan.FromMinutes(30));
                                            }
                                        }
                                    }

                                    if (removeOriginal)
                                    {
                                        FileMarker.RemoveMarkAsNewForAll(file);
                                        fileDao.DeleteFile(file.ID);
                                    }
                                }
                            }
                    }

                    lock (locker)
                    {
                        timer.Change(TIMER_PERIOD, TIMER_PERIOD);
                    }
                }
                catch (Exception exception)
                {
                    Global.Logger.Error(exception.Message, exception);
                    lock (locker)
                    {
                        timer.Change(Timeout.Infinite, Timeout.Infinite);
                    }
                }
                finally
                {
                    Monitor.Exit(singleThread);
                }
            }
        }