コード例 #1
0
        public long UploadFile(File dbFile, Stream stream)
        {
            if (dbFile == null) throw new ArgumentNullException("dbFile");
            if (dbFile.UserId <= 0) throw new ArgumentException("dbFile.UserId <= 0", "dbFile");
            if (stream == null) throw new FileNotFoundException();

            dbFile.Size = stream.Length;
            dbFile.Uploaded = DateTime.Now;

            AddOrUpdateFileInformation(dbFile);
            try
            {
                SaveFileToStorage(dbFile.FileId, stream);
            }
            catch (ErrorLoadingFileException)
            {
                DeleteFileFromStorage(dbFile.FileId);
                DeleteFileFromDatabase(dbFile.FileId);
                throw;
            }

            return dbFile.FileId;
        }
コード例 #2
0
        private void IncrementNumberOfDownloads(File dbFile)
        {
            if (dbFile == null) throw new ArgumentNullException("dbFile");

            dbFile.NumberOfDownloads++;
            _context.Files.Attach(dbFile);
            _context.Entry(dbFile).Property(file => file.NumberOfDownloads).IsModified = true;
            _context.SaveChanges();
        }
コード例 #3
0
        public long AddOrUpdateFileInformation(File dbFile)
        {
            if (dbFile == null) throw new ArgumentNullException("dbFile");
            if (string.IsNullOrEmpty(dbFile.Category)) throw new ArgumentNullException("dbFile.Category");

            string fileName = Path.GetFileName(dbFile.Name);
            //Проверка GetInvalidFileNameChars() т.к GetFileName проверяет только на GetInvalidPathChars()
            string fileNameWithoutExtension;
            if (fileName == null || (fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileName)) == null ||
                fileNameWithoutExtension.Length == 0 || string.IsNullOrWhiteSpace(fileNameWithoutExtension) ||
                fileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1)
                throw new BadFileNameException(dbFile.Name);
            dbFile.Name = fileName.Trim();

            string fileExtension = Path.GetExtension(fileName);
            if (!_allowedFileExtension.Contains(fileExtension))
                throw new NotAllowedFileExtensionException(fileExtension);

            if (_categories.All(category => dbFile.Category != category))
            {
                dbFile.Category = _categories[0];
            }

            if (dbFile.FileId <= 0)
            {
                if (string.IsNullOrWhiteSpace(dbFile.Name)) throw new BadFileNameException("file name is null or emty");
                if (dbFile.UserId <= 0) throw new ArgumentException("dbFile.UserId <= 0", "dbFile");
                if (dbFile.NumberOfDownloads < 0) throw new ArgumentException("dbFile.NumberOfDownloads < 0", "dbFile");
                if (dbFile.Size < 0) throw new ArgumentException("dbFile.Size < 0", "dbFile");

                dbFile.Vote = new Vote();
                _context.Files.Add(dbFile);
            }
            else
            {
                File old = _context.Files.Find(dbFile.FileId);
                var oldEentry = _context.Entry(old);
                if (oldEentry != null)
                {
                    oldEentry.State = EntityState.Detached;
                }

                _context.Files.Attach(dbFile);
                var newEntry = _context.Entry(dbFile);
                if (dbFile.Name != null)
                {
                    newEntry.Property(file => file.Name).IsModified = true;
                }
                if (dbFile.Category != null)
                {
                    newEntry.Property(file => file.Category).IsModified = true;
                }
                newEntry.Property(file => file.Description).IsModified = true;
            }

            _context.SaveChanges();
            return dbFile.FileId;
        }