Skip to content

Commit

Permalink
working get, and a meta path for more build
Browse files Browse the repository at this point in the history
  • Loading branch information
dmikey committed Jul 9, 2021
1 parent a155ecb commit a735530
Show file tree
Hide file tree
Showing 4 changed files with 269 additions and 126 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
"jsonwebtoken": "^5.4.0",
"lodash": "^3.10.1",
"mongodb": "^2.0.46",
"mongoose": "^4.1.11",
"mongoose": "^5.13.2",
"ms": "^0.7.1",
"stream-to-array": "^2.3.0",
"ts-node": "^10.0.0",
"typescript": "^4.3.5"
}
Expand Down
44 changes: 24 additions & 20 deletions src/routes/objects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,32 @@ const store = Store.getStore(
);

export default function (fastify: any) {
fastify.put(
"/:user/:repo/objects/:oid",
function (req: any, res: any, done: any) {
const jwtVerify = checkJWT("upload");
fastify.put("/:user/:repo/objects/:oid", function (req: any, res: any) {
const jwtVerify = checkJWT("upload");

if (req.body) {
(async () => {
await jwtVerify(req, res);
await store.put(
req.params.user,
req.params.repo,
req.params.oid,
req
);
res.code(200).send();
})();
}
if (req.body) {
(async () => {
await jwtVerify(req, res);
await store.put(req.params.user, req.params.repo, req.params.oid, req);
res.code(200).send();
})();
}
});

fastify.get(
"/:user/:repo/objects/:oid/meta",
async function (req: any, res: any) {
res
.code(200)
.send(
await store.getMeta(req.params.user, req.params.repo, req.params.oid)
);
}
);

fastify.get("/:user/:repo/objects/:oid", async function (req: any, res: any) {
const jwtVerify = checkJWT("download");
await jwtVerify(req, res);
// const jwtVerify = checkJWT("download");
// await jwtVerify(req, res);
const size: any = await store.getSize(
req.params.user,
req.params.repo,
Expand All @@ -40,13 +43,14 @@ export default function (fastify: any) {
if (size < 0) {
res.code(404).send();
}
res.set("Content-Length", size);
res.headers("Content-Length", size);
const dataStream: any = await store.get(
req.params.user,
req.params.repo,
req.params.oid
);
dataStream.pipe(res);
res.type(dataStream.fileType.mime);
res.code(200).send(dataStream.fileData);
});
}

Expand Down
85 changes: 77 additions & 8 deletions src/store/siasky_store.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Store } from "./index";
import mongoose from "mongoose";
import FileType from "file-type";
var toArray = require("stream-to-array");

const { SkynetClient } = require("@nebulous/skynet");
const {
Expand All @@ -8,7 +11,25 @@ const {

var _FormData: any = require("form-data");

(SkynetClient as any).prototype.uploadFile = function (stream: any) {
mongoose.connect("mongodb://localhost:27017/gitlfs", {
useNewUrlParser: true,
useUnifiedTopology: true,
});

const fileSchema: any = new mongoose.Schema({
oid: String,
user: String,
repo: String,
skylink: String,
});

const File = mongoose.model("File", fileSchema);
const db = mongoose.connection;

db.on("error", console.error.bind(console, "connection error:"));

// modified to accept buffer
(SkynetClient as any).prototype.uploadFile = async function (stream: any) {
const opts = {
...defaultOptions("/skynet/skyfile"),
portalFileFieldname: "file",
Expand All @@ -19,7 +40,8 @@ var _FormData: any = require("form-data");
};

const formData = new _FormData();
formData.append("data", stream, "test.png");
const fileType = await FileType.fromBuffer(stream);
formData.append("data", stream, `file.${fileType?.ext}`);
const params = {};

return new Promise((resolve, reject) => {
Expand All @@ -39,6 +61,35 @@ var _FormData: any = require("form-data");
});
};

// modified to return buffer
(SkynetClient as any).prototype.downloadFile = function (
path: any,
skylink: any,
customOptions = {}
) {
const opts = {
...defaultOptions("/"),
};

return new Promise((resolve, reject) => {
this.executeRequest({
...opts,
method: "get",
extraPath: skylink,
responseType: "stream",
})
.then((response: any) => {
toArray(response.data).then(function (parts: any) {
const buffers = parts.map((part: any) => Buffer.from(part));
resolve(Buffer.concat(buffers));
});
})
.catch((error: any) => {
reject(error);
});
});
};

export default class SiaSkyStore extends Store {
/**
* Construct S3Store instance
Expand All @@ -55,15 +106,33 @@ export default class SiaSkyStore extends Store {
const skylink = await self._skynet.uploadFile(
Buffer.from(stream.body, "binary")
);
console.log(`Upload successful, skylink: ${skylink}`);
const file = new File({ user, repo, oid, skylink });
file.save(function (err) {
if (err) return console.error(err);
});
return skylink;
}

get(user: any, repo: any, oid: any) {
var self = this;
return new Promise(function (resolve) {
resolve(1);
});
async get(user: any, repo: any, oid: any) {
var self: any = this;
const file: any = await File.find({ oid: `${oid}` });
const skylink = file[0]?.skylink.replace("sia://", "");
const fileData: any = await self._skynet.downloadFile(null, skylink);
const fileType = await FileType.fromBuffer(fileData);
return {
fileType,
fileData,
};
}

async getMeta(user: any, repo: any, oid: any) {
const file: any = await File.find({ oid: `${oid}` });
return file[0]
? {
oid: file[0].oid,
skylink: file[0].skylink.replace("sia://", "https://siasky.net/"),
}
: {};
}

getSize(user: any, repo: any, oid: any) {
Expand Down
Loading

0 comments on commit a735530

Please sign in to comment.