예제 #1
0
        private Stream GetContentStream(DbConnection connection, FileAccess fileAccess = FileAccess.ReadWrite, FileShare fileShare = FileShare.ReadWrite)
        {
            if (ErpSettings.EnableFileSystemStorage && ObjectId == 0)
            {
                var path = DbFileRepository.GetFileSystemPath(this);
                if (File.Exists(path))
                {
                    return(File.Open(path, FileMode.Open, fileAccess, fileShare));
                }

                throw new Exception($"File '{path}' was not found.");
            }
            else
            {
                if (ObjectId == 0)
                {
                    throw new Exception("Trying to get content of a file from database, but it was uploaded to file system. Check FileSystem support configuration.");
                }

                var manager = new NpgsqlLargeObjectManager(connection.connection);
                switch (fileAccess)
                {
                case FileAccess.Read:
                    return(manager.OpenRead(ObjectId));

                case FileAccess.Write:
                    return(manager.OpenRead(ObjectId));

                case FileAccess.ReadWrite:
                    return(manager.OpenReadWrite(ObjectId));
                }
                return(manager.OpenReadWrite(ObjectId));
            }
        }
예제 #2
0
        public async Task <byte[]> GetFileAsync(StoredFile storedFile, DatabaseOptions databaseOptions)
        {
            var fileId = ConvertUtil.DeserializeObject <DatabaseIdentifierOptions>(storedFile.FileIdentifierOptions).FileId;

            byte[] bytes;
            using (var postgreDbConnection = new NpgsqlConnection(databaseOptions.ConnectionString))
            {
                postgreDbConnection.Open();
                var manager = new NpgsqlLargeObjectManager(postgreDbConnection);
                // Reading and writing Large Objects requires the use of a transaction
                using (var transaction = postgreDbConnection.BeginTransaction())
                {
                    // Open the file for reading and writing
                    using (var stream = manager.OpenRead(fileId))
                    {
                        using (var memoryStream = new MemoryStream())
                        {
                            await stream.CopyToAsync(memoryStream);

                            bytes = memoryStream.GetBuffer();
                            memoryStream.Close();
                        }
                    }
                }
            }

            return(bytes);
        }
예제 #3
0
        static void HandleDownload(NetworkStream stream, FileRequestInfo requestInfo)
        {
            var context = new SmartShareContext();
            var file    = context.UploadedFiles.First(x => x.Name == requestInfo.FileName);

            if (file == null)
            {
                stream.Write(new byte[] { FAIL_BYTE });
                return;
            }

            if (!(file.Password == requestInfo.Password))
            {
                stream.Write(new byte[] { FAIL_BYTE });
                return;
            }

            if (file.DownloadsExceeded() || file.IsExpired())
            {
                stream.Write(new byte[] { FAIL_BYTE });
                stream.Close();
                file.Delete(context);
                return;
            }

            stream.Write(new byte[] { ACK_BYTE });
            file.DownloadCount++;
            context.UploadedFiles.Update(file);
            context.SaveChanges();

            var connection = context.Database.GetDbConnection();

            if (connection is NpgsqlConnection)
            {
                var conn = (NpgsqlConnection)connection;
                conn.Open();
                var blobManager = new NpgsqlLargeObjectManager(conn);
                using (var transaction = connection.BeginTransaction())
                {
                    var blobStream = blobManager.OpenRead(file.BlobOid);
                    blobStream.CopyTo(stream);
                    transaction.Commit();
                }
                conn.Close();
                Console.WriteLine("Download successful.");
            }
            else
            {
                throw new NotSupportedException("Unsupported database adapter.");
            }
        }
예제 #4
0
        public async Task <IActionResult> GetDownloadAction(string fileId)
        {
            var file = await _context.StoredFiles.FirstOrDefaultAsync(f => f.Id == Guid.Parse(fileId));

            if (file == null)
            {
                return(new NotFoundResult());
            }

            var dbConnection = _context.Database.GetDbConnection() as NpgsqlConnection;
            var manager      = new NpgsqlLargeObjectManager(dbConnection);

            dbConnection.Open();
            var transaction = dbConnection.BeginTransaction();

            var readStream = manager.OpenRead(file.ObjectId);

            return(new DbDownloadStreamResult(readStream, "application/zip", transaction)
            {
                FileDownloadName = file.FileName
            });
        }