Exemplo n.º 1
0
        public async Task <IActionResult> BasicUpload(IFormCollection formCollection, string libid)
        {
            Library lib = Context.FindObjectById <Library>(libid);

            if (lib == null)
            {
                return(NotFound());
            }

            var files = formCollection.Files;
            List <AssetValue> results = new List <AssetValue>();

            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    var fileExt = Path.GetExtension(formFile.FileName);
                    var fileId  = $"{fileStore.CreateFileId()}{fileExt}";
                    var asset   = new Asset
                    {
                        Name           = formFile.FileName,
                        Library        = lib,
                        CurrentVersion = 1
                    };
                    asset.AssetFiles.Add(new AssetFile
                    {
                        SourceFileName = formFile.FileName,
                        MimeType       = Mime.GetMimeFromExt(fileExt),
                        ResourceId     = fileId,
                        ComponentType  = FileComponent.RootFile,
                        Version        = 1
                    });

                    // add object to the db async.
                    Task addTask = asset.AddToCollection(lib.AssetCollection);

                    // write the content on the current thread
                    using (Stream inStream = formFile.OpenReadStream())
                        fileStore.SaveFile(fileId, inStream);

                    await addTask;
                    results.Add(AssetValue.FromModel(asset));
                }
            }
            return(Ok(results));
        }