public async Task <IHttpActionResult> PostAsync(string parentId = null, string mail = null, bool extract = false, CancellationToken cancellationToken = default(CancellationToken))
        {
            int userId = ApiSecurity.CurrentUserId;
            var files  = new List <FileItem>();
            var parent = await FindFileAsync(parentId, cancellationToken);

            var parenti = parent == null ? default(int?) : parent.Id;
            var manager = new DriveManager(ApiSecurity.Manager, _services, ServerPaths.Map(ServerPaths.DriveFiles));

            using (var content = await Request.Content.ReadAsFileStreamAsync(cancellationToken))
            {
                files.Add(await manager.CreateFromStreamAsync(content.FileStream, parenti, userId, content.FileName, cancellationToken));
            }

            return(Ok(ListResult.Create(files.Select(file => new FileStoreUploadResult
            {
                Name = file.Name,
                Link = new Uri(Request.RequestUri, Url.Route(PublicRootName, new RouteValueDictionary
                {
                    { "userId", file.OwnerId },
                    { "path", file.PhysicalPath }
                }))
                       .AbsoluteUri
            }))));
        }
        /// <summary>
        /// Saves the media stream for the specified <paramref name="portal" />.
        /// </summary>
        /// <param name="portal">The portal which owns the media stream.</param>
        /// <param name="media">The media which contains information on the stream.</param>
        /// <param name="mediaStream">The media stream to save.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual async Task SaveMediaStreamAsync(PortalItem portal, MediaItem media, Stream mediaStream, CancellationToken cancellationToken)
        {
            if (portal == null)
            {
                throw new ArgumentNullException(nameof(portal));
            }
            if (media == null)
            {
                throw new ArgumentNullException(nameof(media));
            }
            if (mediaStream == null)
            {
                throw new ArgumentNullException(nameof(mediaStream));
            }

            var directory = ServerPaths.Map(ServerPaths.PortalMedia, portal.Uri);

            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            using (var fileStream = File.Create(Path.Combine(directory, media.Uri), 4096))
            {
                await mediaStream.CopyToAsync(fileStream);
            }
        }
 public async Task <IHttpActionResult> DeleteAllAsync([FromUri] IEnumerable <int> ids, CancellationToken cancellationToken)
 {
     if (ids != null)
     {
         await(new DriveManager(ApiSecurity.Manager, _services, ServerPaths.Map(ServerPaths.DriveFiles)).DeleteAsync(ids, cancellationToken));
     }
     return(StatusCode(HttpStatusCode.NoContent));
 }
        /// <summary>
        /// Reads the media stream for the specified <paramref name="portal" />.
        /// </summary>
        /// <param name="portal">The portal which owns the media stream.</param>
        /// <param name="media">The media which contains information on the stream.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task <Stream> ReadMediaStreamAsync(PortalItem portal, MediaItem media, CancellationToken cancellationToken)
        {
            if (portal == null)
            {
                throw new ArgumentNullException(nameof(portal));
            }
            if (media == null)
            {
                throw new ArgumentNullException(nameof(media));
            }

            var directory = ServerPaths.Map(ServerPaths.PortalMedia, portal.Uri, media.Uri);

            return(Task.FromResult <Stream>(File.OpenRead(Path.Combine(directory, media.Name))));
        }
        public async Task <IHttpActionResult> PostAsync([FromBody] FileItem model, CancellationToken cancellationToken)
        {
            ModelState.Remove("model.Slug");
            if (model == null || !ModelState.IsValid)
            {
                return(BadRequest());
            }

            var manager = new DriveManager(ApiSecurity.Manager, _services, ServerPaths.Map(ServerPaths.DriveFiles));

            model = await manager.CreateAsync(model, ApiSecurity.CurrentUserId, cancellationToken);

            return(CreatedAtRoute(GetByIdRouteName, new RouteValueDictionary {
                { "id", model.Id }
            }, model));
        }
        /// <summary>
        /// Deletes a media <see cref="Stream" /> from the specified <paramref name="portal" /> as an asynchronous operation.
        /// </summary>
        /// <param name="portal">The portal which owns the media stream.</param>
        /// <param name="media">The media which contains information on the stream.</param>
        /// <param name="cancellationToken">A token to observe while waiting for the task to complete.</param>
        /// <returns>
        /// A task that represents the asynchronous operation.
        /// </returns>
        public virtual Task DeleteMediaStreamAsync(PortalItem portal, MediaItem media, CancellationToken cancellationToken)
        {
            if (portal == null)
            {
                throw new ArgumentNullException(nameof(portal));
            }
            if (media == null)
            {
                throw new ArgumentNullException(nameof(media));
            }

            var fileName = ServerPaths.Map(ServerPaths.PortalMedia, portal.Uri, media.Uri);

            if (File.Exists(fileName))
            {
                File.Delete(fileName);
            }

            return(Task.CompletedTask);
        }
 public async Task <IHttpActionResult> DeleteAsync(int id, CancellationToken cancellationToken)
 {
     //SecurityHelper.Authorize(Services.User, AccessSource.File, id, AccessPermission.IsOwner);
     await(new DriveManager(ApiSecurity.Manager, _services, ServerPaths.Map(ServerPaths.DriveFiles)).DeleteAsync(id, cancellationToken));
     return(StatusCode(HttpStatusCode.NoContent));
 }