Skip to content

Commit

Permalink
1.3.0 add octet stream upload
Browse files Browse the repository at this point in the history
  • Loading branch information
Gary Xue authored and Gary Xue committed Feb 13, 2020
1 parent e2b3271 commit 3de06ab
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 10 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# CHANGELOG.md

## v1.3.0 (2020-02-13)
### Feature: Add octet/stream upload

## v1.2.4 (2019-12-16)
### Bugfix: fix edit menu missing

Expand Down
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ <h1 class="hero-title h2-mobile mt-0 is-revealing">
<div class="control control-expanded">
<a
class="button button-primary button-block button-shadow"
href="https://github.com/garyxuehong/postmate/releases/download/v1.2.4/Postmate-1.2.4.dmg"
href="https://github.com/garyxuehong/postmate/releases/download/v1.3.0/Postmate-1.3.0.dmg"
>Download now</a
>
<p style="display:block; margin-top: 20px;">
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "postmate",
"version": "1.2.4",
"version": "1.3.0",
"private": true,
"main": "public/electron.js",
"homepage": "./",
Expand Down
2 changes: 2 additions & 0 deletions src/model/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class ApiRequest {
url: string = "";
headers: Headers | null = {};
body: string = "";
bodyBuffer: Buffer | null = null;
variablesExtract: VariablesExtract = {};
}

Expand All @@ -49,6 +50,7 @@ export class HttpRequest {
url: string = "";
headers: Headers | null = {};
body: string = "";
bodyBuffer: Buffer | null = null;
cert: ApiCert | null | undefined = undefined;
}

Expand Down
2 changes: 2 additions & 0 deletions src/request-parser/request-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@ export default function parse(
if (body !== "") {
body = varReplace(body, variables);
}
let bodyBuffer = request.bodyBuffer;
const method = varReplace(request.method, variables);
const headers = varHeaderReplace(request.headers || {}, variables);
return {
isHttps,
method,
url,
body,
bodyBuffer,
headers,
cert
};
Expand Down
6 changes: 4 additions & 2 deletions src/request-sender/request-sender.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ export default async function send(
method: request.method,
headers: {
...request.headers,
'content-length': request.body.length //TODO deal with unicode
"content-length": request.bodyBuffer
? request.bodyBuffer.length
: request.body.length //TODO deal with unicode
},
...certOption
};
Expand All @@ -54,7 +56,7 @@ export default async function send(
newRequest.on("error", e => {
reject(e);
});
newRequest.write(request.body, err => {
newRequest.write(request.bodyBuffer || request.body, err => {
if (err) reject(err);
newRequest.end();
});
Expand Down
1 change: 1 addition & 0 deletions src/ui/RequestList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ export default function RequestList({
key={req.name}
onClick={() => {
updateLastClicked(req.name);
if(req.bodyBuffer) req.bodyBuffer = null;
onActivateRequest(req);
}}
>
Expand Down
3 changes: 3 additions & 0 deletions src/ui/RequestOperationPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ export default function RequestOperationPanel({
onBodyChange={body => {
updateTempRequest({ ...tempRequest, body });
}}
onBodyBufferChange={bodyBuffer => {
updateTempRequest({ ...tempRequest, bodyBuffer });
}}
/>
<p>
<br />
Expand Down
39 changes: 33 additions & 6 deletions src/ui/RequestPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,31 @@ import React from "react";
import { ApiRequest } from "../model/model";
import { Form } from "semantic-ui-react";

import fs from 'fs';

const RequestPanel: React.FC<{
request: ApiRequest;
onMethodChange: (value: string) => void;
onUrlChange: (value: string) => void;
onHeadersChange: (key: string, value: string) => void;
onBodyChange: (value: string) => void;
onBodyChange: (value: string ) => void;
onBodyBufferChange: (value: Buffer) => void;
}> = ({
request,
onMethodChange,
onUrlChange,
onHeadersChange,
onBodyChange
onBodyChange,
onBodyBufferChange
}) => {
const headers = request.headers || {};
const isFileUpload =
(
headers["content-type"] ||
headers["Content-Type"] ||
headers["CONTENT-TYPE"] ||
""
).toLowerCase() === "application/octet-stream";
return (
<Form>
<Form.Field>
Expand Down Expand Up @@ -42,10 +54,25 @@ const RequestPanel: React.FC<{
))}
<Form.Field>
<label>Body</label>
<textarea
value={request.body}
onChange={e => onBodyChange(e.target.value)}
/>
{isFileUpload ? (
<input
type="file"
onChange={async e => {
if (e.target.files && e.target.files.length > 0) {
const file = e.target.files[0];
if (file) {
const fileContent = await fs.promises.readFile(file.path);
onBodyBufferChange(fileContent);
}
}
}}
/>
) : (
<textarea
value={request.body}
onChange={e => onBodyChange(e.target.value)}
/>
)}
</Form.Field>
</Form>
);
Expand Down

0 comments on commit 3de06ab

Please sign in to comment.