예제 #1
0
        public async Task <ActionResult <string> > LoadDocument(string id, CancellationToken ct)
        {
            AuthorizeAny(
                () => Actor.IsAdmin,
                () => _svc.CanEdit(id, Actor.Id).Result
                );

            string path = BuildPath();

            path = System.IO.Path.Combine(path, id + ".md");

            if (!System.IO.File.Exists(path))
            {
                return(Ok(""));
            }

            string result = await System.IO.File.ReadAllTextAsync(path, ct);

            if (string.IsNullOrEmpty(result))
            {
                return(Ok(""));
            }

            // string token = new Random().Next().ToString("x");
            // string find = "(\\[.*\\]\\([^)]*)";
            // string replace = $"$1?t={token}";

            // result = Regex.Replace(result, "\\?t=[^)]*", "");
            // result = Regex.Replace(result, find, replace);
            // // add token/id to cache for 30s

            return(Ok(result));
        }
예제 #2
0
        public async Task <ActionResult> Save(string id, [FromBody] string text)
        {
            if (!await _workspaceService.CanEdit(id))
            {
                return(Forbid());
            }

            string path = BuildPath();

            path = System.IO.Path.Combine(path, id + ".md");

            System.IO.File.WriteAllText(path, text);

            return(Ok());
        }
예제 #3
0
        public async Task <ActionResult <Workspace> > LoadWorkspace(string id)
        {
            await Validate(new Entity { Id = id });

            AuthorizeAny(
                () => Actor.IsAdmin,
                () => _svc.CanEdit(id, Actor.Id).Result
                );

            return(Ok(
                       await _svc.Load(id)
                       ));
        }
예제 #4
0
        public async Task <ActionResult <bool> > UploadWorkspaceFile()
        {
            await _uploader.Process(
                Request,
                metadata => {
                string publicTarget = Guid.Empty.ToString();
                string original     = metadata[Meta.OriginalName];
                string filename     = metadata[Meta.Name] ?? original;
                string key          = metadata[Meta.GroupKey] ?? publicTarget;
                long size           = Int64.Parse(metadata[Meta.Size] ?? "0");

                if (_config.MaxFileBytes > 0 && size > _config.MaxFileBytes)
                {
                    throw new Exception($"File {filename} exceeds the {_config.MaxFileBytes} byte maximum size.");
                }

                if (key != publicTarget && !_svc.CanEdit(key, Actor.Id).Result&& !Actor.IsAdmin)
                {
                    throw new InvalidOperationException();
                }

                // Log("uploading", null, filename);
                string dest = BuildDestinationPath(filename, key);
                metadata.Add(Meta.DestinationPath, dest);
                Log("uploading", null, dest);

                return(System.IO.File.Create(dest));
            },
                status => {
                if (status.Error != null)
                {
                    string dp = status.Metadata[Meta.DestinationPath];
                    if (System.IO.File.Exists(dp))
                    {
                        System.IO.File.Delete(dp);
                    }
                }
                _monitor.Update(status.Key, status.Progress);
                // TODO: broadcast progress to group
            },

                options => {
                options.MultipartBodyLengthLimit = (long)((_config.MaxFileBytes > 0) ? _config.MaxFileBytes : 1E9);
            },

                metadata => {
                string dp = metadata[Meta.DestinationPath];

                if (!dp.ToLower().EndsWith(Meta.IsoFileExtension) && System.IO.File.Exists(dp))
                {
                    CDBuilder builder        = new CDBuilder();
                    builder.UseJoliet        = true;
                    builder.VolumeIdentifier = Meta.IsoVolumeId;
                    builder.AddFile(Path.GetFileName(dp), dp);
                    builder.Build(dp + Meta.IsoFileExtension);
                    System.IO.File.Delete(dp);
                }
            }
                );

            return(Json(true));
        }