DocSpace-buildtools/products/ASC.Files/Server/Api/UploadController.cs

214 lines
11 KiB
C#
Raw Normal View History

2022-03-03 17:33:11 +00:00
namespace ASC.Files.Api;
2022-03-14 10:57:59 +00:00
[ConstraintRoute("int")]
public class UploadControllerInternal : UploadController<int>
{
public UploadControllerInternal(UploadControllerHelper<int> filesControllerHelper) : base(filesControllerHelper)
{
}
}
public class UploadControllerThirdparty : UploadController<string>
{
public UploadControllerThirdparty(UploadControllerHelper<string> filesControllerHelper) : base(filesControllerHelper)
{
}
}
public abstract class UploadController<T> : ApiControllerBase
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
private readonly UploadControllerHelper<T> _filesControllerHelper;
2022-03-03 17:33:11 +00:00
2022-03-14 10:57:59 +00:00
public UploadController(UploadControllerHelper<T> filesControllerHelper)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
_filesControllerHelper = filesControllerHelper;
2022-03-03 17:33:11 +00:00
}
/// <summary>
/// Creates session to upload large files in multiple chunks.
/// </summary>
/// <short>Chunked upload</short>
/// <category>Uploads</category>
/// <param name="folderId">Id of the folder in which file will be uploaded</param>
/// <param name="fileName">Name of file which has to be uploaded</param>
/// <param name="fileSize">Length in bytes of file which has to be uploaded</param>
/// <param name="relativePath">Relative folder from folderId</param>
/// <param name="encrypted" visible="false"></param>
/// <remarks>
/// <![CDATA[
/// Each chunk can have different length but its important what length is multiple of <b>512</b> and greater or equal than <b>10 mb</b>. Last chunk can have any size.
/// After initial request respond with status 200 OK you must obtain value of 'location' field from the response. Send all your chunks to that location.
/// Each chunk must be sent in strict order in which chunks appears in file.
/// After receiving each chunk if no errors occured server will respond with current information about upload session.
/// When number of uploaded bytes equal to the number of bytes you send in initial request server will respond with 201 Created and will send you info about uploaded file.
/// ]]>
/// </remarks>
/// <returns>
/// <![CDATA[
/// Information about created session. Which includes:
/// <ul>
/// <li><b>id:</b> unique id of this upload session</li>
/// <li><b>created:</b> UTC time when session was created</li>
/// <li><b>expired:</b> UTC time when session will be expired if no chunks will be sent until that time</li>
/// <li><b>location:</b> URL to which you must send your next chunk</li>
/// <li><b>bytes_uploaded:</b> If exists contains number of bytes uploaded for specific upload id</li>
/// <li><b>bytes_total:</b> Number of bytes which has to be uploaded</li>
/// </ul>
/// ]]>
/// </returns>
[Create("{folderId}/upload/create_session")]
2022-03-14 10:57:59 +00:00
public Task<object> CreateUploadSessionFromBodyAsync(T folderId, [FromBody] SessionRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.CreateUploadSessionAsync(folderId, inDto.FileName, inDto.FileSize, inDto.RelativePath, inDto.LastModified, inDto.Encrypted);
2022-03-03 17:33:11 +00:00
}
[Create("{folderId}/upload/create_session")]
[Consumes("application/x-www-form-urlencoded")]
2022-03-14 10:57:59 +00:00
public Task<object> CreateUploadSessionFromFormAsync(T folderId, [FromForm] SessionRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.CreateUploadSessionAsync(folderId, inDto.FileName, inDto.FileSize, inDto.RelativePath, inDto.LastModified, inDto.Encrypted);
2022-03-03 17:33:11 +00:00
}
/// <summary>
/// Uploads the file specified with single file upload
/// </summary>
/// <param name="folderId">Folder ID to upload to</param>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="title">Name of file which has to be uploaded</param>
/// <param name="createNewIfExist" visible="false">Create New If Exist</param>
/// <param name="keepConvertStatus" visible="false">Keep status conversation after finishing</param>
/// <category>Uploads</category>
/// <returns></returns>
[Create("{folderId}/insert", order: int.MaxValue)]
2022-03-14 10:57:59 +00:00
public Task<FileDto<T>> InsertFileAsync(T folderId, [FromForm][ModelBinder(BinderType = typeof(InsertFileModelBinder))] InsertFileRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.InsertFileAsync(folderId, inDto.Stream, inDto.Title, inDto.CreateNewIfExist, inDto.KeepConvertStatus);
2022-03-03 17:33:11 +00:00
}
2022-03-14 10:57:59 +00:00
/// <summary>
/// Uploads the file specified with single file upload or standart multipart/form-data method to the selected folder
/// </summary>
/// <short>Upload to folder</short>
/// <category>Uploads</category>
/// <remarks>
/// <![CDATA[
/// Upload can be done in 2 different ways:
/// <ol>
/// <li>Single file upload. You should set Content-Type &amp; Content-Disposition header to specify filename and content type, and send file in request body</li>
/// <li>Using standart multipart/form-data method</li>
/// </ol>]]>
/// </remarks>
/// <param name="folderId">Folder ID to upload to</param>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="contentType" visible="false">Content-Type Header</param>
/// <param name="contentDisposition" visible="false">Content-Disposition Header</param>
/// <param name="files" visible="false">List of files when posted as multipart/form-data</param>
/// <param name="createNewIfExist" visible="false">Create New If Exist</param>
/// <param name="storeOriginalFileFlag" visible="false">If True, upload documents in original formats as well</param>
/// <param name="keepConvertStatus" visible="false">Keep status conversation after finishing</param>
/// <returns>Uploaded file</returns>
[Create("{folderId}/upload", order: int.MaxValue)]
public Task<object> UploadFileAsync(T folderId, [ModelBinder(BinderType = typeof(UploadModelBinder))] UploadRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.UploadFileAsync(folderId, inDto);
2022-03-03 17:33:11 +00:00
}
2022-03-14 10:57:59 +00:00
}
public class UploadControllerCommon : ApiControllerBase
{
private readonly GlobalFolderHelper _globalFolderHelper;
private readonly UploadControllerHelper<int> _filesControllerHelper;
public UploadControllerCommon(
GlobalFolderHelper globalFolderHelper,
UploadControllerHelper<int> filesControllerHelper)
{
_globalFolderHelper = globalFolderHelper;
_filesControllerHelper = filesControllerHelper;
}
2022-03-03 17:33:11 +00:00
/// <summary>
/// Uploads the file specified with single file upload to 'Common Documents' section
/// </summary>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="title">Name of file which has to be uploaded</param>
/// <param name="createNewIfExist" visible="false">Create New If Exist</param>
/// <param name="keepConvertStatus" visible="false">Keep status conversation after finishing</param>
/// <category>Uploads</category>
/// <returns></returns>
[Create("@common/insert")]
2022-03-05 14:41:47 +00:00
public async Task<FileDto<int>> InsertFileToCommonFromBodyAsync([FromForm][ModelBinder(BinderType = typeof(InsertFileModelBinder))] InsertFileRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return await _filesControllerHelper.InsertFileAsync(await _globalFolderHelper.FolderCommonAsync, inDto.Stream, inDto.Title, inDto.CreateNewIfExist, inDto.KeepConvertStatus);
2022-03-03 17:33:11 +00:00
}
/// <summary>
/// Uploads the file specified with single file upload to 'Common Documents' section
/// </summary>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="title">Name of file which has to be uploaded</param>
/// <param name="createNewIfExist" visible="false">Create New If Exist</param>
/// <param name="keepConvertStatus" visible="false">Keep status conversation after finishing</param>
/// <category>Uploads</category>
/// <returns></returns>
[Create("@my/insert")]
2022-03-05 14:41:47 +00:00
public Task<FileDto<int>> InsertFileToMyFromBodyAsync([FromForm][ModelBinder(BinderType = typeof(InsertFileModelBinder))] InsertFileRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.InsertFileAsync(_globalFolderHelper.FolderMy, inDto.Stream, inDto.Title, inDto.CreateNewIfExist, inDto.KeepConvertStatus);
2022-03-03 17:33:11 +00:00
}
/// <summary>
/// Uploads the file specified with single file upload or standart multipart/form-data method to 'Common Documents' section
/// </summary>
/// <short>Upload to Common</short>
/// <category>Uploads</category>
/// <remarks>
/// <![CDATA[
/// Upload can be done in 2 different ways:
/// <ol>
/// <li>Single file upload. You should set Content-Type &amp; Content-Disposition header to specify filename and content type, and send file in request body</li>
/// <li>Using standart multipart/form-data method</li>
/// </ol>]]>
/// </remarks>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="contentType" visible="false">Content-Type Header</param>
/// <param name="contentDisposition" visible="false">Content-Disposition Header</param>
/// <param name="files" visible="false">List of files when posted as multipart/form-data</param>
/// <returns>Uploaded file</returns>
[Create("@common/upload")]
2022-03-05 14:41:47 +00:00
public async Task<object> UploadFileToCommonAsync([ModelBinder(BinderType = typeof(UploadModelBinder))] UploadRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-05 14:41:47 +00:00
inDto.CreateNewIfExist = false;
2022-03-03 17:33:11 +00:00
2022-03-14 10:57:59 +00:00
return await _filesControllerHelper.UploadFileAsync(await _globalFolderHelper.FolderCommonAsync, inDto);
2022-03-03 17:33:11 +00:00
}
/// <summary>
/// Uploads the file specified with single file upload or standart multipart/form-data method to 'My Documents' section
/// </summary>
/// <short>Upload to My</short>
/// <category>Uploads</category>
/// <remarks>
/// <![CDATA[
/// Upload can be done in 2 different ways:
/// <ol>
/// <li>Single file upload. You should set Content-Type &amp; Content-Disposition header to specify filename and content type, and send file in request body</li>
/// <li>Using standart multipart/form-data method</li>
/// </ol>]]>
/// </remarks>
/// <param name="file" visible="false">Request Input stream</param>
/// <param name="contentType" visible="false">Content-Type Header</param>
/// <param name="contentDisposition" visible="false">Content-Disposition Header</param>
/// <param name="files" visible="false">List of files when posted as multipart/form-data</param>
/// <returns>Uploaded file</returns>
[Create("@my/upload")]
2022-03-05 14:41:47 +00:00
public Task<object> UploadFileToMyAsync([ModelBinder(BinderType = typeof(UploadModelBinder))] UploadRequestDto inDto)
2022-03-03 17:33:11 +00:00
{
2022-03-05 14:41:47 +00:00
inDto.CreateNewIfExist = false;
2022-03-14 10:57:59 +00:00
return _filesControllerHelper.UploadFileAsync(_globalFolderHelper.FolderMy, inDto);
2022-03-03 17:33:11 +00:00
}
}