Example #1
0
        public async Task <IEnumerable <Cid> > AddAsync(string path,
                                                        bool recursive           = true,
                                                        CancellationToken cancel = default)
        {
            var r = await _nameApi.ResolveAsync(path, cancel : cancel).ConfigureAwait(false);

            var id    = Cid.Decode(r.Remove(0, 6));
            var todos = new Stack <Cid>();

            todos.Push(id);
            var dones = new List <Cid>();

            // The pin is added before the content is fetched, so that
            // garbage collection will not delete the newly pinned
            // content.

            while (todos.Count > 0)
            {
                var current = todos.Pop();

                // Add CID to PIN database.
                await Store.PutAsync(current, new Pin
                {
                    Id = current
                }, cancel).ConfigureAwait(false);

                // Make sure that the content is stored locally.
                await BlockApi.GetAsync(current, cancel).ConfigureAwait(false);

                // Recursively pin the links?
                if (recursive && current.ContentType == "dag-pb")
                {
                    var links = await _objectApi.LinksAsync(current, cancel);

                    foreach (var link in links)
                    {
                        todos.Push(link.Id);
                    }
                }

                dones.Add(current);
            }

            return(dones);
        }
Example #2
0
        public async Task <IEnumerable <Cid> > RemoveAsync(Cid id,
                                                           bool recursive           = true,
                                                           CancellationToken cancel = default)
        {
            var todos = new Stack <Cid>();

            todos.Push(id);
            var dones = new List <Cid>();

            while (todos.Count > 0)
            {
                var current = todos.Pop();
                await Store.RemoveAsync(current, cancel).ConfigureAwait(false);

                if (recursive)
                {
                    if (null != await BlockApi.StatAsync(current, cancel).ConfigureAwait(false))
                    {
                        try
                        {
                            var links = await _objectApi.LinksAsync(current, cancel).ConfigureAwait(false);

                            foreach (var link in links)
                            {
                                todos.Push(link.Id);
                            }
                        }
                        catch (Exception)
                        {
                            // ignored
                        }
                    }
                }

                dones.Add(current);
            }

            return(dones);
        }