示例#1
0
        public async Task HandleDownload(HttpContext context, Guid?documentVersionId, Guid?fileContentId)
        {
            try
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                {
                    sqlConnection.Open();
                    var fileMetadata = GetFileMetadata(documentVersionId, fileContentId, sqlConnection, GetFileNameFromQueryString(context));

                    if (fileMetadata == null)
                    {
                        await _respond.BadRequest(context, "File metadata not found with provided ID.");
                    }
                    else
                    {
                        PopulateHeader(context, fileMetadata.FileName);
                        await ResolveDownload(fileMetadata, sqlConnection, context.Response.Body, context.Response, context);
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "Function PathName is only valid on columns with the FILESTREAM attribute.")
                {
                    await _respond.BadRequest(context, "FILESTREAM attribute is missing from LightDMS.FileContent.Content column. However, file is still available from download via REST interface.");
                }
                else
                {
                    await _respond.InternalError(context, ex);
                }
            }
        }
示例#2
0
        public void HandleDownload(HttpContext context, Guid?documentVersionId, Guid?fileContentId)
        {
            try
            {
                using (var sqlConnection = new SqlConnection(SqlUtility.ConnectionString))
                {
                    sqlConnection.Open();
                    var fileMetadata = GetMetadata(context, documentVersionId, fileContentId, sqlConnection);

                    context.Response.StatusCode = (int)HttpStatusCode.OK;

                    if (!TryDownloadFromAzureBlob(context, fileMetadata))
                    {
                        //If there is no document on AzureBlobStorage, take it from DB
                        context.Response.BufferOutput = false;
                        if (!TryDownloadFromFileStream(context, fileMetadata, sqlConnection))
                        {
                            // If FileStream is not available - read from VarBinary(MAX) column using buffer;
                            DownloadFromVarbinary(context, fileMetadata, sqlConnection);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                if (ex.Message == "Function PathName is only valid on columns with the FILESTREAM attribute.")
                {
                    Respond.BadRequest(context, "FILESTREAM attribute is missing from LightDMS.FileContent.Content column. However, file is still available from download via REST interface.");
                }
                else
                {
                    Respond.InternalError(context, ex);
                }
            }
        }
示例#3
0
        public async Task ProcessRequest(HttpContext context)
        {
            if (context.Request.Form.Files.Count != 1)
            {
                await _respond.BadRequest(context, "Exactly one file has to be sent as request in Multipart format. There were " + context.Request.Form.Files.Count + " files in upload request.");

                return;
            }

            var fileUploadResult = await _uploadHelper.UploadStream(context.Request.Form.Files[0].OpenReadStream());

            switch (fileUploadResult.StatusCode)
            {
            case HttpStatusCode.OK:
                await _respond.Ok(context, new { fileUploadResult.ID });

                break;

            case HttpStatusCode.BadRequest:
                await _respond.BadRequest(context, fileUploadResult.Error);

                break;

            default:
                await _respond.InternalError(context, fileUploadResult.Exception);

                break;
            }
        }
示例#4
0
        public void ProcessRequest(HttpContext context)
        {
            if (context.Request.Files.Count != 1)
            {
                Respond.BadRequest(context, "Exactly one file has to be sent as request in Multipart format. There were " + context.Request.Files.Count + " files in upload request.");
                return;
            }
            var id         = Guid.NewGuid();
            var sw         = Stopwatch.StartNew();
            int bufferSize = 100 * 1024; // 100 kB buffer

            byte[] buffer         = new byte[bufferSize];
            long   totalbytesRead = 0;

            SqlConnection sqlConnection = new SqlConnection(SqlUtility.ConnectionString);

            sqlConnection.Open();
            SqlTransaction sqlTransaction = null;

            try
            {
                sqlTransaction = sqlConnection.BeginTransaction(IsolationLevel.ReadUncommitted);

                SqlCommand checkFileStreamEnabled = new SqlCommand("SELECT TOP 1 1 FROM sys.columns c WHERE OBJECT_SCHEMA_NAME(C.object_id) = 'LightDMS' AND OBJECT_NAME(C.object_id) = 'FileContent' AND c.Name = 'Content' AND c.is_filestream = 1", sqlConnection, sqlTransaction);
                string     createdDate            = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff");

                if (checkFileStreamEnabled.ExecuteScalar() == null)
                {   //FileStream is not supported
                    SqlCommand createEmptyFileContent = new SqlCommand("INSERT INTO LightDMS.FileContent(ID, [Content], [CreatedDate]) VALUES('" + id + "', 0x0, '" + createdDate + "');", sqlConnection, sqlTransaction);
                    createEmptyFileContent.ExecuteNonQuery();
                    SqlCommand fileUpdateCommand = new SqlCommand("update LightDMS.FileContent set Content.WRITE(@Data, @Offset, null) where ID = @ID", sqlConnection, sqlTransaction);

                    fileUpdateCommand.Parameters.Add("@Data", SqlDbType.Binary);
                    fileUpdateCommand.Parameters.AddWithValue("@ID", id);
                    fileUpdateCommand.Parameters.AddWithValue("@Offset", 0);

                    var fileStream = context.Request.Files[0].InputStream;
                    var bytesRead  = fileStream.Read(buffer, 0, buffer.Length);
                    while (bytesRead > 0)
                    {
                        if (bytesRead < buffer.Length)
                        {
                            fileUpdateCommand.Parameters["@Data"].Value = buffer.Where((val, ix) => ix < bytesRead).ToArray();
                        }
                        else
                        {
                            fileUpdateCommand.Parameters["@Data"].Value = buffer;
                        }
                        fileUpdateCommand.Parameters["@Offset"].Value = totalbytesRead;
                        fileUpdateCommand.ExecuteNonQuery();
                        totalbytesRead += bytesRead;
                        bytesRead       = fileStream.Read(buffer, 0, buffer.Length);
                    }

                    fileUpdateCommand.Dispose();
                    fileStream.Close();
                }
                else
                {
                    using (SqlFileStream sfs = SqlFileStreamProvider.GetSqlFileStreamForUpload(id, createdDate, sqlTransaction))
                    {
                        while (totalbytesRead < context.Request.Files[0].ContentLength)
                        {
                            var readed = context.Request.Files[0].InputStream.Read(buffer, 0, bufferSize);
                            sfs.Write(buffer, 0, readed);
                            totalbytesRead += readed;
                        }
                        sfs.Close();
                    }
                }

                sqlTransaction.Commit();
                sqlConnection.Close();
                _performanceLogger.Write(sw, "UploadFile (" + id + ") Executed.");
                Respond.Ok(context, new { ID = id });
            }
            catch (Exception ex)
            {
                try
                {
                    // Try to discard the database transaction (if still open and working).
                    if (sqlTransaction != null)
                    {
                        sqlTransaction.Rollback();
                    }
                    sqlConnection.Close();
                }
                catch
                {
                }

                if (ex.Message == "Function PathName is only valid on columns with the FILESTREAM attribute.")
                {
                    Respond.BadRequest(context, "FILESTREAM is not enabled on Database, or FileStream FileGroup is missing on database, or FILESTREAM attribute is missing from LightDMS.FileContent.Content column. Try with enabling FileStream on database, add FileGroup to database and transform Content column to VARBINARY(MAX) FILESTREAM type.");
                }
                else
                {
                    Respond.InternalError(context, ex);
                }
            }
        }