Skip to content

Commit

Permalink
[release] Sweetfish v0.0.6
Browse files Browse the repository at this point in the history
 * バグ修正
 * Toot Card(リンクなどの詳細を表示する機能)搭載
 * ファイルアップロード時にQIODeviceを使いメモリ最適化
  • Loading branch information
PG-MANA committed Jun 12, 2020
2 parents afa326f + 82198b4 commit 24b5b70
Show file tree
Hide file tree
Showing 21 changed files with 482 additions and 283 deletions.
2 changes: 1 addition & 1 deletion rpmbuild/build.spec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#

%define APP_NAME sweetfish
%define APP_VERSION 0.0.5
%define APP_VERSION 0.0.6
%define APP_HOMEPAGE https://soft.taprix.org/product/sweetfish.html
%define APP_LICENCE Apache License, Version 2.0

Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: sweetfish
version: 0.0.5
version: 0.0.6
base: core18
icon: snap/gui/sweetfish.png
license: Apache-2.0
Expand Down
18 changes: 10 additions & 8 deletions src/Mastodon/MastodonAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
* This software is Licensed under the Apache License Version 2.0
* See LICENSE
*/
#include "MastodonAPI.h"
#include "../Network/Network.h"
#include "../Sweetfish.h"
#include "MastodonAPI.h"
#include "MastodonUrl.h"
#include <QNetworkReply>
#include <QNetworkRequest>
Expand Down Expand Up @@ -283,17 +283,16 @@ QNetworkReply *MastodonAPI::requestUnblock(const QByteArray &user_id) {
* 戻値:結果取得用のQNetworkReply
* 概要:メディアのアップロードを行う。postdataはQHttpMultiPartなどを使いmultipart/form-data形式にすること。
*/
QNetworkReply *MastodonAPI::requestMediaUpload(const QByteArray &data,
QNetworkReply *MastodonAPI::requestMediaUpload(QIODevice &data,
const QByteArray &mime_type) {
QNetworkRequest req;
QList<QByteArrayList> upload_data;

req.setUrl(MastodonUrl::scheme + domain + MastodonUrl::media_upload);
//アップロードリスト作成
upload_data.push_back(
QByteArrayList({"file", "upload" /*暫定*/, mime_type, data}));
//送信
return upload(req, upload_data);
return upload(req, QByteArrayList({"file", "upload" /*暫定*/, mime_type}),
data);
}

/*
Expand Down Expand Up @@ -330,13 +329,16 @@ QNetworkReply *MastodonAPI::post(QNetworkRequest &req, const QByteArray &data) {

/*
* 引数:req(URLなどをセットしたQNetworkRequest),
* data(POSTデータ[QList<QByteArrayList>]) 戻値:受信用QNetworkReply
* info(file_nameやMIMEなどの情報[QByteArrayList])
* data(読み込み用のQIODevice)
* 戻値:受信用QNetworkReply
* 概要:Authorizationヘッダを作成。AccessTokenがないときは使用しない。
*/
QNetworkReply *MastodonAPI::upload(QNetworkRequest &req,
const QList<QByteArrayList> &data) {
const QByteArrayList &info,
QIODevice &data) {
req.setRawHeader("Authorization", "Bearer " + access_token);
return net.upload(req, data);
return net.upload(req, info, data);
}

/*
Expand Down
6 changes: 3 additions & 3 deletions src/Mastodon/MastodonAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class MastodonAPI {
QNetworkReply *requestHomeTimeLine(const QByteArray &since_id = QByteArray());
QNetworkReply *requestUserStream();
//メディア関係
QNetworkReply *requestMediaUpload(const QByteArray &data,
QNetworkReply *requestMediaUpload(QIODevice &data,
const QByteArray &mime_type);
//リスト関係
QNetworkReply *requestGetLists();
Expand All @@ -60,8 +60,8 @@ class MastodonAPI {
QNetworkReply *get(QNetworkRequest &req);
QNetworkReply *post(QNetworkRequest &req, const QByteArray &data);
QNetworkReply *del(QNetworkRequest &req);
QNetworkReply *upload(QNetworkRequest &req,
const QList<QByteArrayList> &data);
QNetworkReply *upload(QNetworkRequest &req, const QByteArrayList &info,
QIODevice &data);

QByteArray user_id;
QByteArray access_token;
Expand Down
18 changes: 11 additions & 7 deletions src/Mastodon/MediaUpload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
* MediaUpload クラス
* MediaUploadに使う。INIT=>APPEND=>FINALIZEと処理していき、media_idを返す。
*/
#include "MastodonAPI.h"
#include "MediaUpload.h"
#include "MastodonAPI.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QNetworkReply>

// MEMO:APIを叩くときはmastodonクラスを使う。(additional_owners対応したら面白いかも。)
MediaUpload::MediaUpload(const QByteArrayList &_list,
const QByteArrayList &mime, MastodonAPI *m,
QObject *parent)
: QObject(parent), list(_list), mimetype(mime), mastodon_api(m), counter(0) {}
MediaUpload::MediaUpload(QList<QIODevice *> _list, const QByteArrayList &mime,
MastodonAPI *m, QObject *parent)
: QObject(parent), list(_list), mimetype(mime), mastodon_api(m),
counter(0) {}

MediaUpload::~MediaUpload() {}

Expand All @@ -27,8 +27,8 @@ bool MediaUpload::start() {
if (mastodon_api == nullptr || !list.size() || !mimetype.size())
return false;
connect(
mastodon_api->requestMediaUpload(list.at(counter), mimetype.at(counter)),
&QNetworkReply::finished, this, &MediaUpload::next);
mastodon_api->requestMediaUpload(*list.at(counter), mimetype.at(counter)),
&QNetworkReply::finished, this, &MediaUpload::next);
return true;
}

Expand All @@ -48,6 +48,10 @@ void MediaUpload::next() {
.toString()
.toUtf8() +
",";

list.at(counter)->close();
delete list.at(counter);

counter++;
if (counter >= list.count()) {
emit finished(id);
Expand Down
9 changes: 6 additions & 3 deletions src/Mastodon/MediaUpload.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,18 @@
#pragma once

#include <QByteArray>
#include <QList>
#include <QObject>

class MastodonAPI;
class QIODevice;

class MediaUpload : public QObject {
Q_OBJECT
public:
explicit MediaUpload(const QByteArrayList &_list, const QByteArrayList &mime,
MastodonAPI *m, QObject *parent = Q_NULLPTR);
explicit MediaUpload(QList<QIODevice *> _list /*upload後にfreeされる*/,
const QByteArrayList &mime, MastodonAPI *m,
QObject *parent = Q_NULLPTR);
virtual ~MediaUpload();
bool start();

Expand All @@ -29,7 +32,7 @@ public slots:
void retry();

private:
QByteArrayList list;
QList<QIODevice *> list;
QByteArrayList mimetype;
QByteArray id; //=media_ids
QByteArray media_id; //操作中のmedia_id
Expand Down
20 changes: 19 additions & 1 deletion src/Mastodon/TootData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,21 @@
//自分の使用しているアカウントのidリスト
QByteArrayList TootData::static_owner_user_id_list;

TootCardData::TootCardData(const QJsonObject &target) {
url = target["url"].toString();
description = target["description"].toString();
type = target["type"].toString();
title = target["title"].toString();
author_name = target["author_name"].toString();
author_url = target["author_url"].toString();
provider_name = target["provider_name"].toString();
provider_url = target["provider_url"].toString();
preview_url = target["image"].toString();
if (preview_url.isEmpty()) {
preview_url = target["embed_url"].toString();
}
}

TootAccountData::TootAccountData(const QJsonObject &target) {
if (target.isEmpty())
return;
Expand Down Expand Up @@ -101,6 +116,9 @@ TootData::TootData(const QJsonObject &target) {

account = TootAccountData(target["account"].toObject());
media = TootMediaData(target["media_attachments"].toArray());
if (!target["card"].isNull()) {
card = TootCardData(target["card"].toObject());
}

flag = 0;
if (target["reblogged"].toBool()) {
Expand Down Expand Up @@ -186,7 +204,7 @@ QString TootData::getApplicationSite() const { return application.second; }
*/
void TootData::analyzeContent(QString c /*remove使うため参照ではない*/) {
// spanとpを消す
c.replace("</p><p>", "\n")
c.replace("</p><p>", "\n\n")
.replace("<br>", "\n")
.replace("<br />", "\n")
.remove(QRegExp("<\\/?(span|p)[^>]*>"))
Expand Down
28 changes: 28 additions & 0 deletions src/Mastodon/TootData.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,32 @@ class TootMediaDataEntry {
friend class TootMediaData;
};

class TootCardData {
public:
TootCardData(){};
explicit TootCardData(const QJsonObject &target);
QString getUrl() const { return url; };
QString getTitle() const { return title; };
QString getType() const { return type; }
QString getAuthorName() const { return author_name; };
QString getAuthorUrl() const { return author_url; };
QString getProviderName() const { return provider_name; };
QString getProviderUrl() const { return provider_url; };
QString getPreviewUrl() const { return preview_url; };
QString getDescription() const { return description; };

private:
QString url;
QString title;
QString type;
QString author_name;
QString author_url;
QString preview_url;
QString description;
QString provider_name;
QString provider_url;
};

class TootMediaData {
public:
TootMediaData(){};
Expand Down Expand Up @@ -124,6 +150,7 @@ class TootData {
TootData *getBoostedData() const { return reblog; };
const TootUrlData &getUrlData() const { return url_list; };
const TootMediaData &getMediaData() const { return media; };
const TootCardData &getCardData() const { return card; };
QDateTime getDateTime() const { return created_at; };
QString getContent() const { return content; };

Expand All @@ -142,6 +169,7 @@ class TootData {
TootAccountData account;
TootUrlData url_list;
TootMediaData media;
TootCardData card;
TootData *reblog = nullptr;

void analyzeContent(QString content);
Expand Down
51 changes: 18 additions & 33 deletions src/Network/Network.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,47 +58,32 @@ QNetworkReply *Network::post(QNetworkRequest &req, const QByteArray &data) {
return qnet.post(req, data);
}

/*
* 引数:url(送信するURL), data(本体、POSTするデータ)
* 戻値:データを受け取るためのQNetworkReplyのポインタ
* 概要:渡されたurlとdataを使ってPOSTリクエストを送る。受信は戻り値であるQNetworkReplyポインタを使う。
*/
QNetworkReply *Network::upload(const QUrl &url,
const QList<QByteArrayList> &data) {
QNetworkRequest req;
req.setUrl(url);
return upload(req, data);
}

/*
* 引数:req(QNetworkRequestで、最低URLは設定しておく),
* data(本体、POSTするデータ) 戻値:データを受け取るためのQNetworkReplyのポインタ
* info(file_nameやMIMEなどの情報[QByteArrayList])
* data(読み込み用のQIODevice)
* 概要:渡されたQNetworkRequestlとdataを使ってmultipart/form-data形式のPOSTリクエストを送る。
* data=>QByteArrayList(0:title, 1:file_name, 2:mime_type, 3:data)
* info=>QByteArrayList(0:title, 1:file_name, 2:mime_type)
*/
QNetworkReply *Network::upload(QNetworkRequest &req,
const QList<QByteArrayList> &data) {
QNetworkReply *Network::upload(QNetworkRequest &req, const QByteArrayList &info,
QIODevice &data) {
req.setHeader(QNetworkRequest::UserAgentHeader, getUserAgent());
QHttpMultiPart *multiformPart =
new QHttpMultiPart(QHttpMultiPart::FormDataType);

for (const QByteArrayList &entry : data) {
if (entry.size() != 4)
continue; //無効
QHttpPart dataPart;
if (!entry.at(1).isEmpty()) {
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"" + entry.at(0) +
"\"; filename=\"" + entry.at(1) + "\""));
} else {
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"" + entry.at(0) + "\""));
}
dataPart.setHeader(QNetworkRequest::ContentTypeHeader,
QVariant(entry.at(2)));
dataPart.setBody(entry.at(3));
multiformPart->append(dataPart);
if (info.size() != 3)
return nullptr; //無効
QHttpPart dataPart;
if (!info.at(1).isEmpty()) {
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"" + info.at(0) +
"\"; filename=\"" + info.at(1) + "\""));
} else {
dataPart.setHeader(QNetworkRequest::ContentDispositionHeader,
QVariant("form-data; name=\"" + info.at(0) + "\""));
}
dataPart.setHeader(QNetworkRequest::ContentTypeHeader, QVariant(info.at(2)));
dataPart.setBodyDevice(&data);
multiformPart->append(dataPart);
QNetworkReply *rep = qnet.post(req, multiformPart);
multiformPart->setParent(rep);
return rep;
Expand Down
5 changes: 2 additions & 3 deletions src/Network/Network.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@ class Network {
QNetworkReply *get(QNetworkRequest &req);
QNetworkReply *post(const QUrl &url, const QByteArray &data);
QNetworkReply *post(QNetworkRequest &req, const QByteArray &data);
QNetworkReply *upload(const QUrl &url, const QList<QByteArrayList> &data);
QNetworkReply *upload(QNetworkRequest &req,
const QList<QByteArrayList> &data);
QNetworkReply *upload(QNetworkRequest &req, const QByteArrayList &info,
QIODevice &data);
QNetworkReply *del(const QUrl &url);
QNetworkReply *del(QNetworkRequest &req);

Expand Down
2 changes: 1 addition & 1 deletion src/Sweetfish.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#define USER_AGENT "Sweetfish for Linux"
#define APP_NAME "Sweetfish"
#define APP_NAME_LONG "Sweetfish for Linux"
#define APP_VERSION "0.0.5"
#define APP_VERSION "0.0.6"
#define APP_HOMEPAGE "https://soft.taprix.org/product/sweetfish.html"
#define APP_COPYRIGHT "Copyright 2018 PG_MANA"
#define DEFAULT_SETTING_FILE_NAME "default.ini"
Loading

0 comments on commit 24b5b70

Please sign in to comment.