예제 #1
0
        public static ChunkedUploadSession UploadChunk(string uploadId, Stream stream, long chunkLength)
        {
            var uploadSession = ChunkedUploadSessionHolder.GetSession(uploadId);

            uploadSession.Expired = DateTime.UtcNow + ChunkedUploadSessionHolder.SlidingExpiration;

            if (chunkLength <= 0)
            {
                throw new Exception(FilesCommonResource.ErrorMassage_EmptyFile);
            }

            if (chunkLength > SetupInfo.ChunkUploadSize)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxUploadSize);
            }

            var maxUploadSize = GetMaxFileSize(uploadSession.FolderId, uploadSession.BytesTotal > 0);

            if (uploadSession.BytesUploaded + chunkLength > maxUploadSize)
            {
                AbortUpload(uploadSession);
                throw FileSizeComment.GetFileSizeException(maxUploadSize);
            }

            using (var dao = Global.DaoFactory.GetFileDao())
            {
                dao.UploadChunk(uploadSession, stream, chunkLength);
            }

            if (uploadSession.BytesUploaded == uploadSession.BytesTotal)
            {
                FileMarker.MarkAsNew(uploadSession.File);
                ChunkedUploadSessionHolder.RemoveSession(uploadSession);
            }
            else
            {
                ChunkedUploadSessionHolder.StoreSession(uploadSession);
            }

            return(uploadSession);
        }
예제 #2
0
        public File CreateOrUpdateFile(File file, Stream stream)
        {
            if (file.FileSize > FileUploader.MaxUploadSize)
            {
                throw FileSizeComment.GetFileSizeException(FileUploader.MaxUploadSize);
            }
            if (String.IsNullOrEmpty(file.FileName))
            {
                throw new ArgumentException(@"name of file cannot be empty", "file");
            }

            file.UserID = SecurityContext.CurrentAccount.ID;
            file.Date   = DateTime.UtcNow;
            file.Version++;
            file.FileLocation = GetFileLocation(file.FileName);

            if (stream != null)
            {
                var store = StorageFactory.GetStorage(CoreContext.TenantManager.GetCurrentTenant().TenantId.ToString(), "wiki");
                store.Save(string.Empty, file.FileLocation, stream, file.FileName);
            }

            return(SaveFile(file));
        }
예제 #3
0
        public File ReplaceFileVersion(File file, Stream fileStream)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }
            if (file.ID == null)
            {
                throw new ArgumentException("No file id or folder id toFolderId determine provider");
            }

            if (SetupInfo.MaxChunkedUploadSize < file.ContentLength)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxChunkedUploadSize);
            }

            if (CoreContext.Configuration.Personal && SetupInfo.IsVisibleSettings("PersonalMaxSpace"))
            {
                if (CoreContext.Configuration.PersonalMaxSpace - Global.GetUserUsedSpace(file.ID == null ? SecurityContext.CurrentAccount.ID : file.CreateBy) < file.ContentLength)
                {
                    throw FileSizeComment.GetPersonalFreeSpaceException(CoreContext.Configuration.PersonalMaxSpace);
                }
            }

            List <object> parentFoldersIds;

            lock (syncRoot)
            {
                using (var tx = dbManager.BeginTransaction())
                {
                    file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title);
                    //make lowerCase
                    file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title));

                    file.ModifiedBy = SecurityContext.CurrentAccount.ID;
                    file.ModifiedOn = TenantUtil.DateTimeNow();
                    if (file.CreateBy == default(Guid))
                    {
                        file.CreateBy = SecurityContext.CurrentAccount.ID;
                    }
                    if (file.CreateOn == default(DateTime))
                    {
                        file.CreateOn = TenantUtil.DateTimeNow();
                    }

                    var sql = Update("files_file")
                              .Set("version", file.Version)
                              .Set("version_group", file.VersionGroup)
                              .Set("folder_id", file.FolderID)
                              .Set("title", file.Title)
                              .Set("content_length", file.ContentLength)
                              .Set("category", (int)file.FilterType)
                              .Set("create_by", file.CreateBy.ToString())
                              .Set("create_on", TenantUtil.DateTimeToUtc(file.CreateOn))
                              .Set("modified_by", file.ModifiedBy.ToString())
                              .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                              .Set("converted_type", file.ConvertedType)
                              .Set("comment", file.Comment)
                              .Set("encrypted", file.Encrypted)
                              .Set("forcesave", (int)file.Forcesave)
                              .Set("thumb", file.ThumbnailStatus)
                              .Where("id", file.ID)
                              .Where("version", file.Version);
                    dbManager.ExecuteNonQuery(sql);
                    tx.Commit();

                    file.PureTitle = file.Title;

                    parentFoldersIds = dbManager.ExecuteList(
                        new SqlQuery("files_folder_tree")
                        .Select("parent_id")
                        .Where(Exp.Eq("folder_id", file.FolderID))
                        .OrderBy("level", false)
                        ).ConvertAll(row => row[0]);

                    if (parentFoldersIds.Count > 0)
                    {
                        dbManager.ExecuteNonQuery(
                            Update("files_folder")
                            .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                            .Set("modified_by", file.ModifiedBy.ToString())
                            .Where(Exp.In("id", parentFoldersIds)));
                    }
                }
            }

            if (fileStream != null)
            {
                try
                {
                    DeleteVersionStream(file);
                    SaveFileStream(file, fileStream);
                }
                catch
                {
                    if (!IsExistOnStorage(file))
                    {
                        DeleteVersion(file);
                    }
                    throw;
                }
            }

            FactoryIndexer <FilesWrapper> .IndexAsync(FilesWrapper.GetFilesWrapper(file, parentFoldersIds));

            return(GetFile(file.ID));
        }
예제 #4
0
        public File SaveFile(File file, Stream fileStream, bool checkQuota = true)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (checkQuota && SetupInfo.MaxChunkedUploadSize < file.ContentLength)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxChunkedUploadSize);
            }

            if (CoreContext.Configuration.Personal && SetupInfo.IsVisibleSettings("PersonalMaxSpace"))
            {
                if (CoreContext.Configuration.PersonalMaxSpace - Global.GetUserUsedSpace(file.ID == null ? SecurityContext.CurrentAccount.ID : file.CreateBy) < file.ContentLength)
                {
                    throw FileSizeComment.GetPersonalFreeSpaceException(CoreContext.Configuration.PersonalMaxSpace);
                }
            }

            var           isNew = false;
            List <object> parentFoldersIds;

            lock (syncRoot)
            {
                using (var tx = dbManager.BeginTransaction())
                {
                    if (file.ID == null)
                    {
                        file.ID           = dbManager.ExecuteScalar <int>(new SqlQuery("files_file").SelectMax("id")) + 1;
                        file.Version      = 1;
                        file.VersionGroup = 1;
                        isNew             = true;
                    }

                    file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title);
                    //make lowerCase
                    file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title));

                    file.ModifiedBy = SecurityContext.CurrentAccount.ID;
                    file.ModifiedOn = TenantUtil.DateTimeNow();
                    if (file.CreateBy == default(Guid))
                    {
                        file.CreateBy = SecurityContext.CurrentAccount.ID;
                    }
                    if (file.CreateOn == default(DateTime))
                    {
                        file.CreateOn = TenantUtil.DateTimeNow();
                    }

                    dbManager.ExecuteNonQuery(
                        Update("files_file")
                        .Set("current_version", false)
                        .Where("id", file.ID)
                        .Where("current_version", true));

                    var sql = Insert("files_file")
                              .InColumnValue("id", file.ID)
                              .InColumnValue("version", file.Version)
                              .InColumnValue("version_group", file.VersionGroup)
                              .InColumnValue("current_version", true)
                              .InColumnValue("folder_id", file.FolderID)
                              .InColumnValue("title", file.Title)
                              .InColumnValue("content_length", file.ContentLength)
                              .InColumnValue("category", (int)file.FilterType)
                              .InColumnValue("create_by", file.CreateBy.ToString())
                              .InColumnValue("create_on", TenantUtil.DateTimeToUtc(file.CreateOn))
                              .InColumnValue("modified_by", file.ModifiedBy.ToString())
                              .InColumnValue("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                              .InColumnValue("converted_type", file.ConvertedType)
                              .InColumnValue("comment", file.Comment)
                              .InColumnValue("encrypted", file.Encrypted)
                              .InColumnValue("forcesave", (int)file.Forcesave)
                              .InColumnValue("thumb", file.ThumbnailStatus);
                    dbManager.ExecuteNonQuery(sql);
                    tx.Commit();

                    file.PureTitle = file.Title;

                    parentFoldersIds = dbManager.ExecuteList(
                        new SqlQuery("files_folder_tree")
                        .Select("parent_id")
                        .Where(Exp.Eq("folder_id", file.FolderID))
                        .OrderBy("level", false)
                        ).ConvertAll(row => row[0]);

                    if (parentFoldersIds.Count > 0)
                    {
                        dbManager.ExecuteNonQuery(
                            Update("files_folder")
                            .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                            .Set("modified_by", file.ModifiedBy.ToString())
                            .Where(Exp.In("id", parentFoldersIds)));
                    }

                    if (isNew)
                    {
                        RecalculateFilesCount(dbManager, file.FolderID);
                    }
                }
            }

            if (fileStream != null)
            {
                try
                {
                    SaveFileStream(file, fileStream);
                }
                catch (Exception saveException)
                {
                    try
                    {
                        if (isNew)
                        {
                            var stored = Global.GetStore().IsDirectory(GetUniqFileDirectory(file.ID));
                            DeleteFile(file.ID, stored);
                        }
                        else if (!IsExistOnStorage(file))
                        {
                            DeleteVersion(file);
                        }
                    }
                    catch (Exception deleteException)
                    {
                        throw new Exception(saveException.Message, deleteException);
                    }
                    throw;
                }
            }

            FactoryIndexer <FilesWrapper> .IndexAsync(FilesWrapper.GetFilesWrapper(file, parentFoldersIds));

            return(GetFile(file.ID));
        }
예제 #5
0
        public File SaveFile(File file, Stream fileStream)
        {
            if (file == null)
            {
                throw new ArgumentNullException("file");
            }

            if (SetupInfo.MaxChunkedUploadSize < file.ContentLength)
            {
                throw FileSizeComment.GetFileSizeException(SetupInfo.MaxChunkedUploadSize);
            }

            var isNew = false;

            lock (syncRoot)
            {
                using (var db = GetDb())
                    using (var tx = db.BeginTransaction())
                    {
                        if (file.ID == null)
                        {
                            file.ID           = db.ExecuteScalar <int>(new SqlQuery("files_file").SelectMax("id")) + 1;
                            file.Version      = 1;
                            file.VersionGroup = 1;
                            isNew             = true;
                        }

                        file.Title = Global.ReplaceInvalidCharsAndTruncate(file.Title);
                        //make lowerCase
                        file.Title = FileUtility.ReplaceFileExtension(file.Title, FileUtility.GetFileExtension(file.Title));

                        file.ModifiedBy = SecurityContext.CurrentAccount.ID;
                        file.ModifiedOn = TenantUtil.DateTimeNow();
                        if (file.CreateBy == default(Guid))
                        {
                            file.CreateBy = SecurityContext.CurrentAccount.ID;
                        }
                        if (file.CreateOn == default(DateTime))
                        {
                            file.CreateOn = TenantUtil.DateTimeNow();
                        }

                        db.ExecuteNonQuery(
                            Update("files_file")
                            .Set("current_version", false)
                            .Where("id", file.ID)
                            .Where("current_version", true));

                        var sql = Insert("files_file")
                                  .InColumnValue("id", file.ID)
                                  .InColumnValue("version", file.Version)
                                  .InColumnValue("version_group", file.VersionGroup)
                                  .InColumnValue("current_version", true)
                                  .InColumnValue("folder_id", file.FolderID)
                                  .InColumnValue("title", file.Title)
                                  .InColumnValue("content_length", file.ContentLength)
                                  .InColumnValue("category", (int)file.FilterType)
                                  .InColumnValue("create_by", file.CreateBy.ToString())
                                  .InColumnValue("create_on", TenantUtil.DateTimeToUtc(file.CreateOn))
                                  .InColumnValue("modified_by", file.ModifiedBy.ToString())
                                  .InColumnValue("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                                  .InColumnValue("converted_type", file.ConvertedType)
                                  .InColumnValue("comment", file.Comment);
                        db.ExecuteNonQuery(sql);
                        tx.Commit();

                        file.PureTitle = file.Title;

                        var parentFoldersIds = db.ExecuteList(
                            new SqlQuery("files_folder_tree")
                            .Select("parent_id")
                            .Where(Exp.Eq("folder_id", file.FolderID))
                            .OrderBy("level", false)
                            ).ConvertAll(row => row[0]);

                        if (parentFoldersIds.Count > 0)
                        {
                            db.ExecuteNonQuery(
                                Update("files_folder")
                                .Set("modified_on", TenantUtil.DateTimeToUtc(file.ModifiedOn))
                                .Set("modified_by", file.ModifiedBy.ToString())
                                .Where(Exp.In("id", parentFoldersIds)));
                        }

                        if (isNew)
                        {
                            RecalculateFilesCount(db, file.FolderID);
                        }
                    }
            }

            if (fileStream != null)
            {
                try
                {
                    SaveFileStream(file, fileStream);
                }
                catch
                {
                    if (isNew)
                    {
                        var stored = Global.GetStore().IsDirectory(GetUniqFileDirectory(file.ID));
                        DeleteFile(file.ID, stored);
                    }
                    else if (!IsExistOnStorage(file))
                    {
                        DeleteVersion(file);
                    }
                    throw;
                }
            }
            return(GetFile(file.ID));
        }