Exemplo n.º 1
0
 public FolderModel Get(int Id)
 {
     return(_folderManager.GetFolderById(new FolderId()
     {
         Value = Id
     }));
 }
Exemplo n.º 2
0
        public async Task <IActionResult> Multiple(IFormFile[] files, int folderId)
        {
            try
            {
                foreach (var file in files)
                {
                    if (file.Length > 0)
                    {
                        var ext = Path.GetExtension(file.FileName).ToLowerInvariant();

                        if (string.IsNullOrEmpty(ext) || !permittedExtensions.Contains(ext))
                        {
                            return(StatusCode(500, "Invalid extension"));
                        }
                        if (file.Length > _fileSizeLimit)
                        {
                            return(StatusCode(500, "Exceeds file limit"));
                        }
                        var folder = _folderManager.GetFolderById(new FolderId()
                        {
                            Value = folderId
                        });
                        if (folder == null)
                        {
                            return(StatusCode(500, "Invalid folder id"));
                        }
                        var      documentKey       = Guid.NewGuid();
                        string   untrustedFileName = file.FileName;
                        DateTime createdDate       = DateTime.UtcNow;
                        //string filePath = GetFilePath(documentKey, folder.Name, file.ContentType, createdDate);
                        //using (var fileStream = new FileStream(filePath, FileMode.CreateNew))
                        //{
                        //    await file.CopyToAsync(fileStream);
                        //}
                        byte[] fileBytes = new byte[0];
                        if (file.Length > 0)
                        {
                            using (var ms = file.OpenReadStream())
                            {
                                using (var ms2 = new MemoryStream())
                                {
                                    await ms.CopyToAsync(ms2);

                                    fileBytes = ms2.ToArray();
                                }
                            }
                        }
                        var request = new DocumentCreateRequest()
                        {
                            Document = new DocumentModel()
                            {
                                ByteSize    = (int)file.Length,
                                Bytes       = fileBytes,
                                FileName    = untrustedFileName,
                                ContentType = file.ContentType,
                                Folder      = folder,
                                DocumentKey = documentKey,
                                Modified    = DateTime.UtcNow,
                                LastViewed  = DateTime.UtcNow,
                                Created     = createdDate
                            }
                        };
                        var result = _documentManager.CreateNewDocument(request);
                        if (!result)
                        {
                            return(StatusCode(500, "Failed to save document"));
                        }
                    }
                }
                return(StatusCode(200));
            }
            catch (Exception ex)
            {
                return(StatusCode(500, ex.Message));
            }
        }